简体   繁体   中英

What are the Android application lifecycle methods? (not activity life cycle methods.)

In my android application, there are 10 activities. I identify all these activities by the number. lets say activity 1 to activity 10.

  1. Activity 1 is splash screen.
  2. Activity 2 = login screen
  3. Activity 3 to 10 are normal pages of book.

Now when I first login to my app, it shows me splash screen and then login screen. My app asks about password. Then it redirects me to activity 3 which is page 1 of book. Now lets assume I am on page 5 ie activity 7 and I suddenly pressed Home button, then application goes to background state.

Now what I want is that when I again open my application and my app in foreground, it will not start activity 7. I want to start activity 2 (login screen) first, then It ask for a correct password. If the user enters the correct password then only activity 7 starts. So basically my question is that is there any way to Identify whether my application will enter to background or did enter in foreground before starting activity? (ie android application life cycle methods.) So that I can launch login activity first through that app life cycle method. This is possible for me in iOS through, app delegate life cycle methods. But how I can do it with android?

This happens because of the Activity LifeCycle. When you close your app by pressing the home button, the onStop() method of the respective Activity which was active at the time is called upon. This method does not destroy the Activity completely, it only stops it.

The Activities in the Android OS are destroyed by calling onDestroy() method of the respective Activity. However, onDestroy() method cannot be called directly because it is a method of Activity's LifeCycle. It gets called automatically by the Android OS. To ensure that Activity's onDestroy() method gets called as soon as your Activity stops, we will call finish() method inside the onStop() method of your respective Activity(s).

@Override
protected void onStop() {
    super.onStop();
    finish();
}

Now, when you'll start your app again, it will start from the beginning ie the splash screen.

Since you do not want to give any headroom to the user and want to log her off as soon as the app goes into background, we can use the general activity life cycle to differentiate when the app when to background and when a screen switch occurred. Your Application class can look like this:

public class MyApplication extends Application implements ActivityLifecycleCallbacks {

    public boolean userLoggedoff;

    ... 

    @Override  
    public void onActivityResumed(Activity activity) {
        Log.i("Activity Resumed", activity.getLocalClassName());

        if (userLoggedoff) 
             startPasswordActivity() // Make sure you add 'CLEAR_TOP' flag to intent
    }

    ... 

}  

Make your 'logged in' activities according to following template:

public class Act1 extends Activity {

    private boolean legitSwitch;

    ...

    // Legit app switch
    private void startNewAct() {
         legitSwitch = true;
         ...
         startActivity()
    }


    protected void onPause() {
         if (!legitSwitch)
              ((MyApplication) getApplication()).userLoggedOff = true;
    }

}

Make sure you set the userLoggedOff boolean to true from your login activity. Hope this helps. Now remember that after pressing the home button and returning, the above method will still show the previous activity for an instant since we are doing stuff on onActivityResumed() method of the application class but will immediately also start the Login Activity because how dare the user press the home button!

You can extend Application class for this purpose. You need to implement Activity lifecycle callbacks there like this:

public class MyApplication extends Application implements ActivityLifecycleCallbacks {

    private boolean userLoggedOff;
    private Handler mHandler;

    ...

    @Override 
    public void onActivityResumed(Activity activity) {
        Log.i("Activity Resumed", activity.getLocalClassName());

        if (userLoggedoff)
             startPasswordActivity()
        else {
             mHandler.cancelCallbacksAndMessages(null);
             userLoggedOff = false;
        }
    } 

    @Override 
    public void onActivityPaused(Activity activity) {
        Log.i("Activity Paused", activity.getLocalClassName());

        mHandler.postDelayed(new Runnable() {
             @Override
             public void run() {
                  userLoggedOff = true;
             }
        }, 5000);
    } 

    ...

} 

Now, here what we are trying to is that as soon as onPause() of any activity is called we trigger a delayed event using handler to logging the user off. This event can be cancelled only when onResume() of any activity is called within 5 seconds (like you know, this may be called during regular activity switching or also when the app comes from the background but).

This might solve your issue elegantly, I guess.

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