finish() and the Activity lifecycle
Asked Answered
B

5

13

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().

Biedermeier answered 29/9, 2012 at 19:44 Comment(0)
B
7

You are correct. onPause, onStop, onDestroy.

Here are the docs.

Baran answered 29/9, 2012 at 19:50 Comment(2)
Thanks for that link, I didn't actually see that series before (I've been looking at the API docs only). Raises some other questions, though - for instance, I thought that onDestroy() would be used as a "destructor," not onStop(); and that all persistent data should be written in onPause(). Learn something new every day, I guess...Biedermeier
NO! NOT EXACTLY see: #12656398Austenite
A
8

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)

Austenite answered 14/5, 2015 at 1:4 Comment(2)
First, the link is outdated. Here's a current version: developer.android.com/reference/android/app/… Second, shouting "NO!" is a pretty rude way to answer a question, especially when you do it both in your own answer, and in a bunch of replies to other peoples' answers. Having said that, yes, this is correct.Biedermeier
I agree that the giant "NO" is unpleasant, however I think the information in the post is very relevant and something people would want to see when finding this question. I am personally up voting it.Louvain
B
7

You are correct. onPause, onStop, onDestroy.

Here are the docs.

Baran answered 29/9, 2012 at 19:50 Comment(2)
Thanks for that link, I didn't actually see that series before (I've been looking at the API docs only). Raises some other questions, though - for instance, I thought that onDestroy() would be used as a "destructor," not onStop(); and that all persistent data should be written in onPause(). Learn something new every day, I guess...Biedermeier
NO! NOT EXACTLY see: #12656398Austenite
A
3

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

Amberambergris answered 29/9, 2012 at 19:52 Comment(3)
Those are excellent lesser-known methods but I don't believe there is an onPostPause().Scagliola
Thanks. I will investigate. I'm learning there's a lot that's not mentioned in most Android books.Biedermeier
NOT EXACTLY #12656398Austenite
R
1

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.

Raouf answered 29/9, 2012 at 19:59 Comment(0)
T
0

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

}
Trifling answered 29/9, 2012 at 20:42 Comment(2)
The problem with this suggestion (and Jan Koester's) is that I can't test for situations when the Android system automatically destroys an Activity in order to free resources. Or if there is a way, I don't know what it is.Biedermeier
You can use Java's finalize method. Before the GC destroys an object it will make a call to this method. Use a system-out or a call to LogCat. In addition, check out the Settings and Dev Tools in the Android emulator, there are some handy features that may help you. Here's the link: developer.android.com/tools/debugging/debugging-devtools.htmlTrifling

© 2022 - 2024 — McMap. All rights reserved.