I change Linear Layout background image via this code:
mainlayout.setBackgroundResource(R.drawable.back);
But i want do this on every time that activity starts, In first start and switching between activities. I put this code inside an IF statement in onCreate() but background not change! Of course when i set this code to a button works fine! How and where i have to put my code?
my complete code is:
//check theme id
if(myDbHelper.gettheme()==1)
{
mainlayout.setBackgroundResource(R.drawable.back);
}else if(myDbHelper.gettheme()==2)
{
mainlayout.setBackgroundResource(R.drawable.blueback);
}
You need to put your code into the onResume() method.
Here are details that will explain why
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
You may want to refer to the Android activity lifecycle, but I'd recommend the onResume() method.
I'm assuming based off your code snippet you have the image you want as a background as a drawable resource already in project. If so you could just go into the XML and add into the linerayout:
android:background="#drawable/back"
This should just set the background within the XML layout avoiding having to have code set it within one of the activity life-cycle functions.
Though in the case you wanted different themes as I just saw in the original post (seemed to be edited since I was typing this up), you could try storing the constant int of the R.drawable that the user wants as the background or theme, and have your DB Helper's getTheme() return that reference to set the background without the if statements.
Such saying the user changes their preference to R.drawable.black or .blueback store the int within the DB so it get return by gettheme rather than a 1 or 2 enumeration. Not sure if this would be a best practice though.
EDIT: Are you sure theme is either 1 or 2?
If you want to do it everytime, why don't you just define it as mainLayout's background in your layout.xml? Otherwise, use onResume() rather than onCreate().
Related
I have an Activity which can open 2 different Fragments by 2 different Buttons. By the default that Activity when it creates, it is opening a Fragment, we call it "The Main Fragment".
The first Fragment to which we are going over by the first Button we meet zero problems with the Rotation, but the second one after the rotation disappears and the screen shows the Main Fragment's content. When I tried to rotate the screen back, I see the Main Fragment's content again. But why it is so, if I didn't write any code, which must return me to the Main Fragment without clicking a button.
What assumptions do you have?
Why this is happening ?
Default Behavior, Actiivty is getting recreated on orientation change so your fragment are.
Explanation
You need to understand Activity Life Cycle to understand why this is happening.
First, “rotating the screen” is not the actual scenario we are talking about today. Because any configuration change will cause Android to restart your Activity. A configuration change might be the device rotating (because now we have a different screen layout to draw upon), or it could be a language switch (because we need to re-write all those strings, which may need more room now OR it could be the scary RTL switch!), or even keyboard availability.
By reloading your app, what the system is actually doing is calling onDestroy() and then immediately calling onCreate(). This way, your Activity is as fresh as possible, with all of the right creation data (even though the user has been with you the entire time).
Now you have following option -
Either Fix Orientation for your app from AndroidManifest.xml
But oviously that is not a very good experience for user.
Save activityState with onSaveInstanceState()
This method will be called before onDestroy(). And, when your Activity is created, there’s a matching step onRestoreInstanceState(), which will also be called automatically. All of these automatic steps mean that you can let the system worry about saving and loading your data, because you planned ahead and mapped out what was important. (Or, you can skip onRestoreInstanceState() and load your saved state from the Bundle that comes with onCreate().
In you integrate Fragment in activity, because activity is getting destroy() so your fragment will also destroy() and will be recreated.
Please take a good read on Handling Configuration Change and this.
Once you understood the concepts things will start falling into your but it will only happen if you will complete your learning curve.
Happy Coding !
That is because onCreate is being called every time the screen is rotated. Probably you are displaying The Main Fragment from your onCreate method. You will face the same issue if you put your fragment display logic in onResume because just after onCreate, onResume is called.
Solution: store the fragment on top in shared preferences that way you know what to display every time onCreate is being called.
I am working on an android application so I need to check if the user is at some activity say "xyz" then proceed or change background colour etc. How can I check the current activity in if condition like
if (xyz activity){
set Background Color
// Do something
}
How can I do this in runtime so that whenever user goes to any screen this condition becomes true and then the below code works?
Try this:
if(getActivity() instanceof TheWantedActivity){
//...
}
If i follow your question than put your background code in your OnResume(); if the activity is in foreground its onResume always called.
In each activity or "screen" a user can see or interact with, a class is designated to that view. So if you want to set the background to activity xyz, you do not need to check if the user is there. You need to just load the background you specify within the onCreate(Bundle bundle) method with "setContentView(R.layout.your_activity_layout)". This layout should be stored in your res/layout folder after you create an activity. So for example, if your activity is named StartActivity.java, the layout file saved in res/layout will most likely be named activity_start.xml.
If the answer Saurabh provided isn't promising enough you can take
ActivityLifecycleCallbacks in your Application class, It will only work API 14 and above
When my program start I would like to do some settings before is really starting. Forexample choose the user, check the updates and so on. After these settings I would like to start the main program with the appropriate.
Which is the best way to do this?
You can run an AyncTask, or multiple if you need one for each check, in your onCreate() and show a ProgressDialog while the data is being fetched then cancel it in onPostExecute() and move on to the rest of the MainActivity depending on the data that is downloaded. If you need help getting started with AsyncTask you can see this SO answer on the basic structure.
If you use a ProgressDialog then the app will still start but the users will see something and know that data is loading so they won't feel like it is freezing or taking too long to load (or at least they will know why it isn't loaded right away).
AsyncTask Docs
Edit after comment
For what you said you want in your comment you can do this easily with an Activity that has a Dialog Theme. This will give you the functionality you need (a couple Buttons and store the values) but it will look like a little popup. You can't use an actual Dialog as they need an Activity, the same with any menus, AFAIK. Just create your Activity and make it the launcher and main using the Intent-filters then also add the following line to that Activity's tag in the manifest
android:theme="#android:style/Theme.Dialog"
This approach should give you what you need
There are numerous ways to do that.
First - your app is doing some heavy stuff and this may be freezing user interface. In that version do:
1. Create and activity on what you will override onCreate method and set some content with a spinner - so something will be alive and user will see that something is being done.
2. after you will compute all the things that your app need and may I suggest write it to some global variables override onStart method in what change layout to what suit you and give a user a great UI!
Second - you app is not heavy lifting here just throw everything into override of onStart method.
Handy material here for educating:
I have a static textview that has a certain default value set in the XML. Later, I set that variable to something else, but whenever the orientation changes, the value of the static variable returns to the default, even though I'm not reinitializing it. Why might this be? Shouldn't the value persist even after onCreate() is called?
When onCreate() gets called I'm guessing you also call setContentView() as normal. This redraws all of the Views with the default values. You need to handle the configuration changes
See this answer of mine on how to handle it. The recommended way is to use onSavedInstanceState but I normally handle it myself and haven't had any problems...yet
This has to do with the lifecycle of an Android app. Whenever you rotate, your application goes through all the lifecycle method calls. You need to store the current state within a Bundle and restore that data in your onCreate after the rotation has occurred.
During orientation change Android re-creates all layouts. To save the state of the widget you can simply assign an id to the TextView element:
<TextView
android:id="#+id/tv_caption"
...
/>
Hi and thanks in advance for your time and attention.
I actually have 2 questions i'm not too sure about building an android app. Pretty basic stuff I believe:
1) My app is fully on Horizontal mode, like AngryBirds for example. When it starts the user figures out he should hold the phone like that, if he isn't already. And that is setup in the manifest for all the activities and works fine. but is there a way to prevent the physical device tilting to call onCreate again? can i override it's method or whatever? the reason i'm asking, is because i have a few ButtonViews that after you click on them, change their picture. i am using onRetainNonConfigurationInstance() to save the array of those ImageButtons, and i even mark the ones changed with the ImageButton setTag() and getTag() methods, so when onCreate is called because of the device tilt it gets the saved array from getLastNonConfigurationInstance() , but i've been trying to make it work for quite some time now and I just can't get it right. After the device tilt (I'm actually using the emulator so it's Ctrl+F11 but i believe it will happen with a device as well) all of the ImageButtons loose their pictures.. Long story short - are there better ways of doing this that you can recommend or is preventing the tilt from doing anything is possible or better?
2) What is the best way to know how many seconds the user has been on a screen? I tried to use two longs that i get via SystemClock.currentThreadTimeMillis() as follows: get the starting time onCreate, and the ending time on the method i call to move to the second intent just before i startActivity. but I think because they are called from different threads, the endingpoint - startingpoint is not correct. What is the way to insure both of the methods of SystemClock.currentThreadTimeMillis() are called from the same thread, the main thread of the activity that i'm stopwatching? Is there a better way to go around this?
Thanks again.
You are doing the right to handle orientation change. Please refer to this link http://developer.android.com/guide/topics/resources/runtime-changes.html . This will help you to get it working.
Good way would be to count the time between onResume and onPause. OnCreate is not called all the time if you are resuming activity.
1) You can try adding the property android:configChanges="orientation" to your activity in the manifest file. It worked for me when my dynamic Action Bar tabs got messed up upon rotation of the screen.
You need specify orientation in android manifest for each of your activities, it will not call onCreate then. android:screenOrientation look at http://developer.android.com/guide/topics/manifest/activity-element.html
User see and interact with your activity starting since onResume and ends on onPause. Other time it does not guarantee that user actually see and can click on something in the activity. System.getCurrentMillis() is good enough.