2

I'm wondering is there any programming way to recognize whether an activity resumes from/followed by the onStart() or onPause()?

Android activity life cycle - what are all these methods for?

Thanks in advance.

EDIT:

My question might be not clear enough. Actually, for example, I want to show toast when my activity resumes from onPause but not onStart, is it possible? Thanks for your time.

Community
  • 1
  • 1
PhiVuTru
  • 43
  • 5
  • Why would you want a programming way? Just look at the chart, it tells you the order – Tim Jun 27 '16 at 08:04
  • protected void onCreate(Bundle savedInstanceState) {...} savedInstanceState is used to find that out. Store something in the state before pausing your activity & check the state in onCreate. – Luca Nicoletti Jun 27 '16 at 08:04
  • @LucaNicoletti Thank for the hint Luca. However, as I understand, onPause is not followed by onCreate, so by what way can I put something in the state bundle before pausing? (Sorry for my broken English, I hope you see my point) – PhiVuTru Jun 27 '16 at 08:23
  • Have a look at onRestoreInstanceState() & onSaveInstanceState() – Luca Nicoletti Jun 27 '16 at 08:30
  • check this link from google https://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle – MBH Jun 27 '16 at 08:40
  • @MBH: link in the question I posted already explains clearly. Thanks for your reply. – PhiVuTru Jun 27 '16 at 08:44
  • android documented the whole functions in a table where you can understand it very clearly as well, i recommend you to check out that table in the link i sent... good luck – MBH Jun 27 '16 at 08:46

3 Answers3

1

Maintain a flag in your activity. In onPause set it to true and in onResume check the value and perform tasks accordingly. Set the flag back to false in the end of onResume

user3215142
  • 326
  • 2
  • 6
  • Thank you. This did the trick. I tried applying this earlier, but last time it didn't work, don't know why :D. Thanks again. – PhiVuTru Jun 27 '16 at 08:49
0

I guess what you need is Log class (https://developer.android.com/reference/android/util/Log.html). This is its tutorial for Android Studio: https://developer.android.com/studio/debug/am-logcat.html

It seems like you want to trace the on- methods for some reason, so simply write:

protected void onStart() {
    Log.d("YourTag", "onStart() is called.");
    super.onStop();
}

protected void onPause() {
    Log.d("YourTag", "onPause() is called.");
    super.onPause();
}

or this is the utility method I use to trace methods:

public static void currentMethod() {
        Throwable t = new Throwable();
        Log.d("SomeTag", "==== " + t.getStackTrace()[1].getClassName() + "#" + t.getStackTrace()[1].getMethodName()
                + " ====");
}
Romulus Urakagi Ts'ai
  • 3,699
  • 10
  • 42
  • 68
0

yes there any programming way to recognize whether an activity resumes from/followed by the onStart() or onPause() .

To understand it i am following with a simple notification application :

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);
///it will come in action after code of display notification function described below
DisplayNotification("ActivityLifeCycleDemo","onCreate");

sleepForaMinute();


}

}

Now we will add two more supporting functions in our activity .

static final int NOTIFICATION_ID = 1776;

protected void DisplayNotification(String title,String message)

{

NotificationManager notifManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

Notification note = new Notification(R.drawable.icon, title, System.currentTimeMillis());

PendingIntent intent = PendingIntent.getActivity(this, 0, new Intent(this, ActivityLifeCycleDemo.class), 0);

note.setLatestEventInfo(this, title, message, intent);

notifManager.notify(NOTIFICATION_ID, note);

}

protected void sleepForaMinute()

{

try

{

Thread.sleep(60000);

} catch (InterruptedException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}

}

Now, when you start your activity, you should see the notification shown below on your Android device.

onDestroy

Now let’s override the onDestroy event so that all resources being cleanas follows:

@Override
protected void onDestroy() {

super.onDestroy();

DisplayNotification("ActivityLifeCycleDemo","onDestroy");

}

The onDestroy method should clean up all the resources those were acquired by the onCreate method and previously used by the now-destroyed activity.

onStart

Now let’s override the onStart method as follows:

@Override
protected void onStart() {
super.onStart();
DisplayNotification("ActivityLifeCycleDemo","onStart");
sleepForaMinute();
}

Now here In the onStart method, you should start all the visible actions necessary for your activity, like showing animations, displaying user prompts, etc. onStop Now, let’s override the onStop method as follows:

@Override
protected void onStop() {
super.onStop();
DisplayNotification("ActivityLifeCycleDemo","onStop");
}

The onStop method should stop all of the actions that were started in the onStart method. Because the activity is invisible, your activity should not be performing (and consuming CPU cycles for) any tasks required for the Android interface. onResume

Now let’s override the onResume method as follows:

protected void onResume() {

super.onResume();

DisplayNotification("ActivityLifeCycleDemo","onResume");

}

onPause

Now let’s override the onPause method as follows:

@Override
protected void onPause() {
super.onPause();
DisplayNotification("ActivityLifeCycleDemo","onPause");
}

Conclusion

An Android activity goes through several different states during its lifetime. Understanding the states and the events will help you to code your app in a more efficient, responsive way for your users. The Android operating system, by calling events on the activity, lets the activity manage itself quite effectively, as long as you’re developing the different events carefully. So, have fun while managing the life cycle of your next Android app!

Shubham Sharma
  • 2,763
  • 5
  • 31
  • 46