Issue
My app switches between different activities, with each activity being killed after switching to another one. This works totally fine, but what I am trying to do is, when the user kills the app (either by pressing the Back key or in some other way), that its last running activity is be the next time the app is launched.
I tried ...
onSaveInstanceState(Bundle bundle)
... but this does not seem to work across other activities.
Is there another way of doing this? I also thought about directly editing the manifest, but concluded that it would probably not work and would be very dirty.
Solution
From what I understand, for example, you have 3 activities: ActivityA
, ActivityB
and ActivityC
. If the user exits the app via ActivityB
, and later starts the app up again, you want ActivityB
to start first.
You can do this on two ways.
First, you can have a single Activity
with a placeholder for one of 3 fragments in it. Then you can substitute the fragment as needed.
Second, you can store the last opened activity in preferences, using SharedPreferences
. You can do this in the onStart()
of each ActivityX
you have, and read it off when you determine which activity to start.
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
// save:
Editor edit = prefs.edit();
edit.putString("lastActivity", getClass().getName());
edit.commit();
// restore later:
String activityName = prefs.getString("lastActivity", YourActivityA.getClass().getName());
// if (activityName == null) activityName = YourActivityA.getClass().getName(); /// start that activity as needed
Answered By - Oleg Vaskevich
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.