Android finish during onStop doesn't trigger onDestroy - android

I created a basic sample application that all it does is call finish() during the Activity onStop(). Based on my understanding and from looking at other posts on Stackoverflow, this should trigger onDestroy().
My repro steps are launch the application, press the Home button on the device, which will trigger onStop(). The expected result is that it also hits onDestroy(), but I'm not seeing that.
If I call finish() during onPause() instead, it will trigger onStop() and onDestroy() as expected.
Anyone know why it would work during onPause() but not onStop()?
I've also noticed this message in the Logcat
I/ActivityManager: Activity reported stop, but no longer stopping: ActivityRecord{203ab0 u0 com.amazon.myapplication/.MainActivity t96 f}
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("Sample", "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onStart() {
Log.d("Sample", "onStart");
super.onStart();
}
#Override
protected void onResume() {
Log.d("Sample", "onResume");
super.onResume();
}
#Override
protected void onPause() {
Log.d("Sample", "onPause");
super.onPause();
}
#Override
protected void onStop() {
Log.d("Sample", "onStop");
finish();
super.onStop();
}
#Override
protected void onDestroy() {
Log.d("Sample", "onDestroy");
super.onDestroy();
}

Related

how to check if android going to recreate activity or destroy?

I have an android studio project. When I am rotating screen, android destroys and recreates main activity. How can I check during the destruction, if android going to recreate activity?
You can determine if the activity is finishing by user choice (user chooses to exit by pressing back for example) using isFinishing() in onDestroy.
#Override
protected void onDestroy() {
super.onDestroy();
if (isFinishing()) {
// wrap stuff up
} else {
//It's an orientation change.
}
}
Another alternative (if you're only targeting API>=11) is isChangingConfigurations.
#Override
protected void onDestroy() {
super.onDestroy();
if (isChangingConfigurations()) {
//It's an orientation change.
}
}
Override the Activity lifecycle methods to see the flow.And then use the appropriate method to check activity current state like isChangingConfigurations()
Example code snippet.
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onStart() {
super.onStart();
Log.i(MainActivity.class.getSimpleName(),"OnStart Called");
}
#Override
protected void onRestart() {
super.onRestart();
Log.i(MainActivity.class.getSimpleName(),"OnRestart Called");
}
#Override
protected void onDestroy() {
super.onDestroy();
Log.i(MainActivity.class.getSimpleName(),"OnDestroy Called");
}
#Override
protected void onPause() {
super.onPause();
Log.i(MainActivity.class.getSimpleName(),"OnPause Called");
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.i(MainActivity.class.getSimpleName(),"OnConfiguration Changed Called");
}
}
For more details see the official page activity-lifecycle

How to call onDestroy() from onStop() after a delay

If the user clicks on the Home button for example, the methods onPause() and onStop() are called.
I want to call onDestroy() from the onStop() method after 1mn, unless the user goes back on the app (which calls onResume() and onStart() methods).
I tried to implement Timer:
It fails, saying it cannot call the onDestroy if Looper not implemented.
When I implement the Looper, the onDestroy() method is never called.
Maybe calling onDestroy() from onStop() is not the good thing to do, and another "clean" solution exists to get the behavior I want. I just want to kill the app after 1mn no use.
In this case, please propose.
If my wish is the good way to proceed, could you share how to implement it ?
dont call onDestroy() directly , instead call finish() after the period you want
and to support the scenario you mentioned make sure not to kill the activity if the user resumed the activity
here's a piece of code i wrote for you .
the activity will kill its self if not resumed in 1 second ;
boolean notResumed;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startActivity(new Intent(this,Main2Activity.class));
}
#Override
protected void onResume() {
super.onResume();
notResumed=false;
}
#Override
protected void onStop() {
super.onStop();
notResumed=true;
Handler handler=new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
if(notResumed)
finish();
}
},1000);
}
#Override
protected void onDestroy() {
super.onDestroy();
Log.d("debug","onDestroyCalled");
}
This answer is largely inspired from Abdelrahman's post above.
I just adapted few things to reinitialize the delay counter each time I go out of my app.
boolean notResumed;
//Declare my Handler in global to be used also in onResume() method
Handler myHandler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startActivity(new Intent(this,Main2Activity.class));
}
#Override
protected void onResume() {
super.onResume();
notResumed=false;
//Remove callbacks on the handler if it already exists
if (myHandler != null) {
//I send null here to remove all callbacks, all messages,
//and remove also the reference of the runnable
myHandler.removeCallbacks(null);
}
}
#Override
protected void onStop() {
super.onStop();
notResumed=true;
myHandler=new Handler();
myHandler.postDelayed(new Runnable() {
#Override
public void run() {
if(notResumed)
finish();
}
},10000);
}
#Override
protected void onDestroy() {
super.onDestroy();
Log.d("debug","onDestroyCalled");
}
Thanks a lot again to Abdelrahman Nazeer for its fast and accurate answer.
Please comment if something is not correctly done here. At least it works as expected...

When I call startActivity() in onCreate(),did the other lifecycle method execute [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
When I call the startActivity() method to start other activity on the onCreate() method;
Did the other lifecircle method execute,like onStart() or onResume()
I had a test
AppMain.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_main);
Log.i(TAG, "onCreate");
startActivity(new Intent(AppMain.this,AppOther.class));
}
#Override
protected void onRestart() {
Log.i(TAG, "onRestart");
super.onRestart();
}
#Override
protected void onStart() {
Log.i(TAG, "onStart");
super.onStart();
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
Log.i(TAG, "onRestoreInstanceState");
super.onRestoreInstanceState(savedInstanceState);
}
#Override
protected void onResume() {
Log.i(TAG, "onResume");
super.onResume();
}
#Override
protected void onPause() {
Log.i(TAG, "onPause");
super.onPause();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
Log.i(TAG, "onSaveInstanceState");
super.onSaveInstanceState(outState);
}
#Override
protected void onStop() {
Log.i(TAG, "onStop");
super.onStop();
}
#Override
protected void onDestroy() {
Log.i(TAG, "onDestroy");
super.onDestroy();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
Log.i(TAG, "onConfigurationChanged");
super.onConfigurationChanged(newConfig);
}
AppOther.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_other);
Log.i(TAG, "onCreate");
}
#Override
protected void onRestart() {
Log.i(TAG, "onRestart");
super.onRestart();
}
#Override
protected void onStart() {
Log.i(TAG, "onStart");
super.onStart();
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
Log.i(TAG, "onRestoreInstanceState");
super.onRestoreInstanceState(savedInstanceState);
}
#Override
protected void onResume() {
Log.i(TAG, "onResume");
super.onResume();
}
#Override
protected void onPause() {
Log.i(TAG, "onPause");
super.onPause();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
Log.i(TAG, "onSaveInstanceState");
super.onSaveInstanceState(outState);
}
#Override
protected void onStop() {
Log.i(TAG, "onStop");
super.onStop();
}
#Override
protected void onDestroy() {
Log.i(TAG, "onDestroy");
super.onDestroy();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
Log.i(TAG, "onConfigurationChanged");
super.onConfigurationChanged(newConfig);
}
Logcat:
05-29 05:28:11.583: I/AppMain(1257): onCreate
05-29 05:28:11.614: I/AppMain(1257): onStart
05-29 05:28:11.614: I/AppMain(1257): onResume
05-29 05:28:11.643: I/AppMain(1257): onSaveInstanceState
05-29 05:28:11.643: I/AppMain(1257): onPause
05-29 05:28:11.793: I/AppOther(1257): onCreate
05-29 05:28:11.793: I/AppOther(1257): onStart
05-29 05:28:11.793: I/AppOther(1257): onResume
05-29 05:28:12.383: I/AppMain(1257): onStop
I don't know why the onStart() and onResume() method can still execute;
It's seems that startActivity() did not break the AppMain's lifecycle
Language is pale, speak in code.
Just test it.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v(this.getClass().toString(),"onCreate");
//start other Activity
this.startActivity(new Intent(this,OtherActivity.class));
}
#Override
protected void onDestroy() {
super.onDestroy();
Log.v(this.getClass().toString(),"onDestroy");
}
#Override
protected void onPause() {
super.onPause();
Log.v(this.getClass().toString(),"onPause");
}
#Override
protected void onResume() {
super.onResume();
Log.v(this.getClass().toString(),"onResume");
}
#Override
protected void onStart() {
super.onStart();
Log.v(this.getClass().toString(),"onStart");
}
}
And the logcat shows:
Question's way too narrow but if you start another activity, the current activity will toggle onPause(). and if you finished the called activity the previous activity will toggle onResume(). In short it will not call onResume() and onStart() of current activity if you start another acitivity.

Kill single activity Android app

I'm an Android newbie. I'm writing an app to trace the Activity lifecycle using Log statements. I want to kill my app in order to see the onDestroy() event being called. I've added a button that calls finish to do this, but I've not been able to terminate the app. I've also tried System.exit(0), but my app won't terminate. What am I doing wrong?
public class MainActivity extends Activity {
private static final String LOG_DISPLAY = "DEBUG";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(LOG_DISPLAY, "onCreate called");
}
#Override
protected void onPause() {
super.onPause();
Log.d(LOG_DISPLAY, "onPause called");
}
#Override
protected void onResume() {
super.onResume();
Log.d(LOG_DISPLAY, "onResume called");
}
#Override
protected void onStop() {
super.onStop();
Log.d(LOG_DISPLAY, "onStop called");
}
#Override
protected void onStart() {
super.onStop();
Log.d(LOG_DISPLAY, "onStart called");
}
#Override
protected void onRestart() {
super.onStop();
Log.d(LOG_DISPLAY, "onRestart called");
}
public void addListenerOnButton() {
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
finish();
//System.exit(0);
}
});
}
}
You must override onDestroy in order to check if it is called, and call your addListenerOnButton method too:
Try with this:
public class MainActivity extends Activity {
private static final String LOG_DISPLAY = "DEBUG";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(LOG_DISPLAY, "onCreate called");
}
#Override
protected void onDestroy() {
super.onDestroy();
Log.d(LOG_DISPLAY, "onDestroy called");
}
#Override
protected void onPause() {
super.onPause();
Log.d(LOG_DISPLAY, "onPause called");
}
#Override
protected void onResume() {
super.onResume();
Log.d(LOG_DISPLAY, "onResume called");
}
#Override
protected void onStop() {
super.onStop();
Log.d(LOG_DISPLAY, "onStop called");
}
#Override
protected void onStart() {
super.onStop();
Log.d(LOG_DISPLAY, "onStart called");
}
#Override
protected void onRestart() {
super.onStop();
Log.d(LOG_DISPLAY, "onRestart called");
}
public void addListenerOnButton() {
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
MainActivity.this.finish();
//System.exit(0);
}
});
}
}
First add missing onDestroy implementation to show you logs :
#Override
protected void onDestroy() {
Log.d(LOG_DISPLAY, "onDestroy called");
super.onDestroy();
}
Then simply open activity and exit it with hardware back button. You will see onDestroy logs.
You will have same effect calling finish() programmatically as well, just dont forget to place call to your addListenerOnButton somewhere within onCreate

Killing process in onPause()

I want to kill my Activity process when I pause it by answering a call or something like that
but when i try to start my app it closes instantly. Any solutions? Sample code below
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Things to do
}
//#Override
public void onPause() {
super.onPause();
android.os.Process.killProcess(android.os.Process.myPid());
}
You should implement a logical scenario based on activity's lifecycle methods.
update your code with
#Override
public void onPause() {
super.onPause();
this.finish();
}

Categories

Resources