2

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.

Community
  • 1
  • 1
cokelemon
  • 21
  • 1
  • 2
  • do you mean open last activity when the user opens the app again? – Sadiq Md Asif Jan 10 '17 at 04:02
  • "but clicking on a previous activity returns me a blank screen." what does that mean? – Sadiq Md Asif Jan 10 '17 at 04:11
  • @SadiqMdAsif An entirely white, blank activity. The 2nd link I mentioned in the question had the answerer who said that it was because DispatcherActivity was empty, so it was showing DispatcherActivity. – cokelemon Jan 10 '17 at 04:15
  • you mean `backbutton` press? then `override` the `onBackPressed` method to switch to your desired `Activity` – Sadiq Md Asif Jan 10 '17 at 04:19
  • @SadiqMdAsif not backbutton. Eg: I launch the app from **1st Activity**, click to **2nd Activity**, then close the app on the **2nd Activity**. I reopen the app, it loads on **2nd Activity**. If I click a button to go **3rd Activity**, it shows properly, but if I click a button to go **1st Activity** from the 2nd Activity, it's a **white activity**. – cokelemon Jan 10 '17 at 04:24
  • what's the code are your using to go to backActivity ? are you using new intent and then startActivty() ? – Sadiq Md Asif Jan 10 '17 at 04:27
  • @SadiqMdAsif from 2nd Activity, I use finish(). My 1st Activity's onResume only contains sharedPreferences for retaining text in EditText – cokelemon Jan 10 '17 at 04:31
  • finish will close the app as after restart/relaunch as this is the first one in the Stack! use intent to start previousActivity . This will do the work\ – Sadiq Md Asif Jan 10 '17 at 04:32
  • @SadiqMdAsif Ahhh I see. Thanks! I will try that. So I should not use finish() at all to navigate? – cokelemon Jan 10 '17 at 04:35
  • welcome. you can do it if you are in a flow and just finished doing any work on an Activity and just need to 'get back' not `go back` to the previous activity. – Sadiq Md Asif Jan 10 '17 at 04:42
  • It works! Thanks! How do I reward you? – cokelemon Jan 10 '17 at 04:55

0 Answers0