Your app will always start from the Activity
with the filter intent.action.MAIN
in the Manifest.xml.
And if your application is already running then the next time time your app starts it will automatically resume from the last opened activity.
Now if your app gets killed or swiped out then you can store the activity in SharedPreferences
when activity is resumed or paused and on next start check in your spash which is Activity 'A'
in your case, which was the last opened activity as you had stored the Activity
name in SharedPreferences
.
Example :
If you have three activities A-->B-->C
store the name in onPause()
of all activities use SharedPreference
to store its name :
@Override
protected void onPause() {
super.onPause();
SharedPreferences prefs = getSharedPreferences("Pref", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("lastOpenedActivity", getClass().getName());
editor.apply();
}
And in your Splash which is here Activity A
use this preference to checfk in your onCreate()
-
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Class lastActivity;
try {
SharedPreferences prefs = getSharedPreferences("Pref", MODE_PRIVATE);
lastActivity = Class.forName(prefs.getString("lastOpenedActivity", ParentActivity.class.getName()));
} catch (ClassNotFoundException ex) {
lastActivity = ParentActivity.class;
}
startActivity(new Intent(this, lastActivity));
this.finish();
}