简体   繁体   中英

Relaunch app when resuming from background

Can I prevent app from resuming when it's killed by other process?

I have problem where I have Singleton class instance in Application class , it holds through app all my info about user and stuff gathered from API . When I open app from background(if it's killed by some process) I getting null on User Model(Picture paths, Name...) and app crushes.

Best way for me is app relaunch from launcher screen and get all info again, not resuming. Any solution for this?

Can I prevent app from resuming when it's killed by other process?

There are three ways an app can be "resumed", by a call to either onCreate , onStart , or onResume . Which one is called depends on how your app was "suspended", and what happens to it while in this state.

onResume follows onPause , which occurs when another app is brought to the foreground.

onStart ( onRestart ) follows onStop , which occurs when your app is no longer visible.

onCreate follows onPause or onStop if your memory is lost to another app.

When I open app from background(if it's killed by some process) I getting Nulls on User Model(Picture paths, Name...)

Try persisting this user data in onSaveInstanceState

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putString(STATE_USER, mUser);
    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

and restoring it in either onCreate or onRestoreInstanceState

static final String STATE_USER = "user";
private String mUser;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        // Restore user data from saved state
        mUser = savedInstanceState.getString(STATE_USER);
    }
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onRestoreInstanceState(savedInstanceState);
    text.setText(savedInstanceState.getString(STATE_USER));
}

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