To avoid OnDestory() and OnCreate() beeing called all the time when navigating between my main activity and some sub activities, I have set the singleTop option in my manifest. Unfortunatly this causes that the UI in my main activity is gone when returning from a sub activity. Do I really need to redraw my UI manually?
I am still wondering how to handle these basic navigation features. Is it unusual to perform application initialization tasks (e.g. start services) in OnCreate() of the main activity?
I must be missing something from your explanation I guess because the very basic point that you provided is not correct. When you launch another (sub) activity from your activity then your starting activity is not destroyed but it is stopped. So your onStart is called when you navigate back.
So maybe you can explain a little bit more the background of your requirements.
Use android:launchMode="singleTask" for your main activity so the system will not re-create it if you call it via Intent. You can also clear the stack and go back to your main activity by using
Intent intent = new Intent(SubActivity.this, MainActivity.class);
intentHome.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intentHome.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intentHome);
Related
Is there a way to launch an activity when returning to an application after it was terminated to free up memory? Android is attempting to start the previous activity, but some data I need has been cleaned up during termination. If I can push my home activity onto the stack when returning, my app will be able to function properly.
Thanks!
Declare a public static variable somewhere, which is set to true when your main activity has completed its initialization.
In your other activities, in onCreate(), check if this variable is set to true. If not, it means that Android has killed and recreated your process and restarted just the top activity. In this case, just launch the main activity using Intent.FLAG_ACTIVITY_CLEAR_TOP. This will clear the task stack and start your main activity.
This will only work if the main activity is always in the task stack, at the very bottom (root activity). This means that your main activity should NOT call finish() when it launches other activities on top of itself.
Try this:
put an EXTRA value into the intent starting the second activity, anything really, just to indicate it was started from your MainActivity. Then, in the second Activity's onCreate() use getIntent() to obtain the intent that started it. If it contains your extra value, do nothing and let the activity start normally. If it does not, finish() the second Activity and start MainActivity.
My app has a launcher activity that looks at the intent used to call it and calls one of many activities based on the intent (let's call the child activity B). The launcher activity also launches an AsyncTask to do some clean up in SharedPreferences in the background. Once that background task finishes, the launcher activity calls finish() to terminate itself.
If the background task is still not finished when activity B terminates, I want to avoid returning to the launcher activity. How can I do that?
I am thinking of using android:noHistory="true" in the definition of the activity in the manifest. However, the description of noHistory says:
"Whether or not the activity should be removed from the activity stack and finished (its finish() method called) when the user navigates away from it and it's no longer visible on screen". So I am afraid that the activity might be terminated before it finishes running its background task.
Is my background task guaranteed to be allowed to run to completion? Is there some better way to do this?
Make is simple. Start a background task in your application instance and clean-up preferences there. This will allow you to finish your "dispatcher" activity immediately after it started a next required activity.
Regarding excluding the activity from the back stack. There are two options. You can either define "dispatcher" activity with android:noHistory="true" in manifest file, or start "dispatcher" activity with FLAG_ACTIVITY_NO_HISTORY. Both options will exclude the activity from back stack.
Also, if "dispatcher" activity is in background, Android might decide to destroy it at any time. This might lead to unexpected results. With proposed soliton you have no such issue either.
I want my activity the recognize when it is left and a new activity is started so for example when i'll do
startActivity(intent);
It will perform a certain code.
I tried using onPause();
But it only work on leaving the activity manually
Why don't you perform the operation before calling the new startActivity() and go read about the android activity life cycle to understand how it works
EDIT::
if i truly understand what you are saying..then i think you are looking for onStop() method
Look at the Android activity lifecycle here. onPause is called when "another activity comes to the foreground". If I understand your question correctly, it seems like overriding your first activity's onPause method will work. It's called when you add a new activity on the back stack or when you send the app to the background.
For convenience, here's the chart from the link I provided that I refer to for activity lifecycle questions:
So I have two activites where I can switch between. Let's call them ActivityA and ActivityB.
Now if I'm in ActivityB and it somehow crashes and I start my App again it should appear the ActivityB and not A.
I thought of SharedPreferences(I already used it for the switching) and savedInstanceState, but can the last one handle with the savedInstanceState of other activites or just by its own?
Can I somehow tell ActivityA to look if there is already an instance of ActivityB and if so ro start it immediately?
When an app starts, Android always runs whichever activity it is told to (as defined in the manifest) - which can work to your advantage. You should be able to create a splash activity that determines what the last activity was (via tracking, sharedPrefs or perhaps savedInstanceState - I haven't mucked with the latter much), and then launch the appropriate activity before finish();-ing the splash activity.
Make sure the splash looks decent though - add in a handler that will keep it up for at least 1500-2000ms.
I have an Activity with a lot of fragments transactions in it, and sometimes my users need to open the preferences Activity for a few seconds. I do it like this :
Intent preferencesIntent = new Intent(this, PreferencesView.class);
this.startActivity(preferencesIntent);
Each time I do this, onDestroy() of my main Activity is called. So when my user are coming back, the main Activity is in it initial stage. But I would like the main Activity to be like when they leave it.
I know that I should save/restore my data and just let the Activity recreate itself. But this would be very complicated, and my users are in my main Activity most of the time.
So is there a way to tell Android not to kill my main Activity while it is not visible?
Thank you in advance!
You've alreay written the answer: save/restore state. This is how android works.
Your activity can be destroyed configuration change or something else.
You can't prevent Android to stop an Activity.
The only way I see is to load the preferences in a Fragment in the main Activity.