PhiVuTru
PhiVuTru

Reputation: 43

Android Activity Life Cycle - know if onResume followed by onStart or onPause?

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.

Upvotes: 2

Views: 1271

Answers (3)

Shubham Sharma
Shubham Sharma

Reputation: 2793

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!

Upvotes: 0

user3215142
user3215142

Reputation: 326

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

Upvotes: 1

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()
                + " ====");
}

Upvotes: 0

Related Questions