Question

I'm learning Android programming for a class, and I have a quick question about how finish() fits into the Activity lifecycle.

When you make a call to finish(), what lifecycle callback is started? I presume it's onPause(), then onStop() and onDestroy(). Is this correct?

Really, I just want to make sure that it doesn't jump straight to onDestroy().

Was it helpful?

Solution

You are correct. onPause, onStop, onDestroy.

Here are the docs.

OTHER TIPS

Really, I just want to make sure that it doesn't jump straight to onDestroy(). ???

NO!

but there is one exception when calling finish() result in activity lifecycle break this happens when u call finish() from onCreate() method in which case onDestroy() will be immediately called!

http://developer.android.com/reference/android/app/Activity.html#onCreate(android.os.Bundle)

Yes, it will not jump to onDestroy() skipping the onPause and onStop.

Also you might be interested in onPostResume() ,onPostPause() ,onPostCreate(),onUserLeaveHint(), etc .... These are not listed out in the activity life cycle

It could also be very interesting for you to analyze such problems and issues. You can for example set a debuggin-breakpoint in the onPause() method and investigate the program flow from this point.

Also some print-outs can give you some helpful information.

You could for example write System.out.println("name of the method" + " called."); in each method of your activity which you think is called. (Overwrite for example onPause(), call super.onPause() and place a console print-out to see if the method is called.

You will learn a lot about the Android system doing such little investigations while you develop.

Create a new Android App and place this in the main activity.

Then view the LogCat window (under Android's DDMS) for the outputs

Build you application the same − add all the onPause, onStop, etc. methods with outputs to the LogCat.

As your program runs you can monitor what is called and at what times.

package com.app.myapp;


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;


public class MyApp extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        LinearLayout layout = new LinearLayout(this); 

        Button exit = new Button(this);
        exit.setText("finish");
        exit.setOnClickListener(new Button.OnClickListener()
        {
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                Log.v("MyApp", "finish");
                finish();
            }
        });

        layout.addView(exit);

        setContentView(layout);

        Log.v("MyApp", "onCreate");
    }

    @Override
    protected void onDestroy()
    {
        // TODO Auto-generated method stub
        super.onDestroy();

        Log.v("MyApp", "onDestroy");
    }

    @Override
    protected void onPause()
    {
        // TODO Auto-generated method stub
        super.onPause();

        Log.v("MyApp", "onPause");
    }

    @Override
    protected void onRestart()
    {
        // TODO Auto-generated method stub
        super.onRestart();

        Log.v("MyApp", "onRestart");
    }

    @Override
    protected void onResume()
    {
        // TODO Auto-generated method stub
        super.onResume();

        Log.v("MyApp", "onResume");
    }

    @Override
    protected void onStart()
    {
        // TODO Auto-generated method stub
        super.onStart();

        Log.v("MyApp", "onStart");
    }

    @Override
    protected void onStop()
    {
        // TODO Auto-generated method stub
        super.onStop();

        Log.v("MyApp", "onStop");
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top