I need to be able to reopen last opened activity after the app is closed, and after the phone is restarted.
I was referring to this and this. In the 2nd link, I faced the same problem as OP in the comments of the marked answer but the solution did not work for me.
I am able to launch the app with the last open Activity, but clicking on a previous activity returns me a blank screen. After adding finish() to DispatcherActivity as 2nd link suggested, it closes the app with a log message:
W/IInputConnectionWrapper: showStatusIcon on inactive InputConnection
I have searched for solutions to that error but they involved using 'connection.close()' or HTTP problems which were not relevant.
I have also tried alternatives like this but they did not work either.
Regarding reopening the last opened screen after the phone is restarted, this answer stated that onSavedInstanceState would not work for a restarted phone.
This is the code for my AndroidManifest
<activity
android:name=".Activity.TakeoverMainActivity">
</activity>
<activity android:name=".Activity.TakeoverNextActivity" />
...
<activity android:name=".Activity.DispatcherActivity"
android:launchMode="singleTop"
android:windowSoftInputMode="stateVisible|adjustResize"
android:alwaysRetainTaskState="false">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
This is the code for my DispatcherActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dispatcher);
Class<?> activityClass;
try {
SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE);
activityClass = Class.forName(
prefs.getString(LAST_ACTIVITY, TakeoverMainActivity.class.getName()));
} catch(ClassNotFoundException ex) {
activityClass = TakeoverMainActivity.class;
}
Intent intent = new Intent(this, activityClass);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
This is the onPause methods of my other activities
@Override
protected void onPause() {
super.onPause();
SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE);
SharedPreferences.Editor activityEditor = prefs.edit();
activityEditor.putString(LAST_ACTIVITY, getClass().getName());
activityEditor.commit();
}
Any solutions or suggestions would be greatly appreciated. Thank you.
UPDATE: SadiqMdAsif has solved my problem in the comments. My issue was that I used finish() on my 2nd Activity instead of starting a new intent.