Reputation: 167
I want to restore the last activity when the user close the application. But I dont know what to do. I need your help... anyone?
I have 2 activities, and I want it to be so that if the user has been viewing the 2nd activity and then closes the application, he would still view the last activity when he open the application again.
This is my activity1:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String lastActivity =
PreferenceManager.getDefaultSharedPreferences(this).getString("last_activity", "");
if (last_activity == WelcomeActivity.getSimpleName())
{
startActivityForResult(new Intent(this, WelcomeActivity.class), mDay,
savedInstanceState);
} else {
startActivityForResult(new Intent(this, MainActivity.class), mDay,
savedInstanceState);
}
}
public void onActivityResult() {
finish();
}
And here is my activity 2:
public class WelcomeActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
}
@Override
public void onResume() {
Editor e = PreferenceManager.getDefaultSharedPreferences(this).edit();
e.putString("last_activity", getClass().getSimpleName());
e.commit();
super.onResume();
}
}
I just want it to restore on the last viewed activity when the user open the application, but I can't. Please help me, I am new in android.
Upvotes: 2
Views: 158
Reputation: 11194
Simply Save the State of Activties True or False in shared pref and onCreate method check if which acctivies status is true open that activity
1.Declare variables
SharedPreferences pref;
SharedPreferences.Editor editor;
2.in onCrete method
pref = getSharedPreferences("testapp", MODE_PRIVATE);
editor = pref.edit();
3. When user comes on 1st Activity
editor.putInt("MyActivity",1);
editor.commit();
4.When user comes on 2st Activity
editor.putInt("MyActivity",2);
editor.commit();
Then every time u can check by :
int getStatus=pref.getInt("MyActivity",-1);
if(getStatus==1) redirect(using intent) to 1st activity else show 2nd activity and finish the current activity
Upvotes: 0
Reputation: 6867
Override onStop()
method in each Activity
:
@Override
public void onStop() {
Editor e = PreferenceManager.getDefaultSharedPreferences(this).edit();
e.putString("last_activity", getClass().getSimpleName());
e.commit();
}
Then override onCreate()
and onRestart()
methods in each Activity
and there put:
String lastActivity =
PreferenceManager.getDefaultSharedPreferences(this).getString("last_activity", "");
if (last_activity == WelcomeActivity.getSimpleName()) {
startActivityForResult(new Intent(this, WelcomeActivity.class), mDay,
savedInstanceState);
} else if(last_activity == "") {
// Do nothing,
} else {
startActivityForResult(new Intent(this, MainActivity.class), mDay,
savedInstanceState);
}
This way whenever user leaves the application, or Android decides to close it, you will have your Activity
saved in SharedPreferences
.
Upvotes: 2