简体   繁体   中英

Starting another activity when one activity is about to start: Android

I have an app where I have to show the Splash Screen every time the user comes to the app. That is whether he launches the app afresh or whthere he foregrouds he app, or whether he clicks on the launcher icon.

The issue is that after the splash screen i also want to go back to the last activity that was on visible when he left the app (in case it is not a fresh start)

To solve this I extended Application and implemented the ActivityLifecycleCallbacks.

SO when my app was in the bacground and came back to foregroud, I would launch the Splash screen from

@Override
    public void onActivityStarted(Activity activity) {
     //launch splash screen
}

Now once the splash screens work is done i kill it and I am back to whatever activity was there when the user last using the app before the app went to the background.

Here it seems to be working fine but i keep getting the following error:

09-14 16:44:46.746: E/ActivityThread(5622): Performing pause of activity that is not resumed: {SignInActivity}
09-14 16:44:46.746: E/ActivityThread(5622): java.lang.RuntimeException: Performing pause of activity that is not resumed: {SignInActivity}
09-14 16:44:46.746: E/ActivityThread(5622):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3015)
09-14 16:44:46.746: E/ActivityThread(5622):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3003)
09-14 16:44:46.746: E/ActivityThread(5622):     at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2981)
09-14 16:44:46.746: E/ActivityThread(5622):     at android.app.ActivityThread.access$1000(ActivityThread.java:135)
09-14 16:44:46.746: E/ActivityThread(5622):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
09-14 16:44:46.746: E/ActivityThread(5622):     at android.os.Handler.dispatchMessage(Handler.java:102)
09-14 16:44:46.746: E/ActivityThread(5622):     at android.os.Looper.loop(Looper.java:136)
09-14 16:44:46.746: E/ActivityThread(5622):     at android.app.ActivityThread.main(ActivityThread.java:5001)
09-14 16:44:46.746: E/ActivityThread(5622):     at java.lang.reflect.Method.invokeNative(Native Method)
09-14 16:44:46.746: E/ActivityThread(5622):     at java.lang.reflect.Method.invoke(Method.java:515)
09-14 16:44:46.746: E/ActivityThread(5622):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
09-14 16:44:46.746: E/ActivityThread(5622):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
09-14 16:44:46.746: E/ActivityThread(5622):     at dalvik.system.NativeStart.main(Native Method)

So I have two questions:

  1. What do i do so that this error does not come.
  2. Is there a better way to handle the flow that i mentioned above: that i have to show the splash screen and then take the sure to the last visible activity?

Thanks, Sunny

Into the Oncreate Method you can start an ActivityForResult

startActivityForResult(intent, activity);

and then follow your activity with:

protected void onActivityResult(int requestCode, int resultCode, Intent data)

If I understand correctly you want to show the last activity even after the app has been completely destroyed/terminated. You will need persistent data to achieve that I think. I used shared preferences below as an example (haven't tested it but I hope you get the idea).

//Inside every activity of your app
@Override
public void onResume(){
    super.onResume();
    //Store the current activity name inside shared preferences
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putString("LastActivity", getCurrentActivityName());
    editor.commit();
    //Show splash screen here every time the app is brought to foreground (aka it resumes)
}

private void openActivityFromName(String name){
    Class<?> c = null;
    if(name != null) {
        try {
            c = Class.forName(name);
        } catch (ClassNotFoundException e) {            
            e.printStackTrace();
        }
    }
    Intent intent = new Intent(this, c);
    startActivity(intent);
}

private String getCurrentActivityName(){
    this.getClass().getSimpleName()
}

/**
* Inside your main activity (the one that opens first normally)
*/
@Override
protected void onCreate(Bundle state){
    super.onCreate(state);   
    // Restore preferences
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    String lastActivity = settings.getString("LastActivity", "");
    //Open the last opened activity everytime the app opens 
    openActivityFromName(lastActivity);
}

Bringing your app to the background (not closing it) will not close your current activity. So the behavior of opening the last activity is only needed once the app has been destroyed completely and onCreate is called again.

I had the same problem. I think you should just put onResume method and eventually onPause() method to control state of your activity.

put onResume() method

@Override
protected void onResume()
{
 super.onResume();
...//
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM