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();
}
Related
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
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();
}
I have really hard time with background music in my app. I just want to play music in all activities - when I press home button I want to stop music. I want "stop or play" music button in all activities, but couldnĀ“t make it work.
So I decided to make embarassing choice - play it only in 1 activitiy by
onCreate
backgroundmusic = MediaPlayer.create(StoryActivity.this, R.raw.creepy_music);
backgroundmusic.start();
onPause
#Override
protected void onPause() {
super.onPause();
backgroundmusic.release();
finish();
}
Can you please help me with easy activity lifecycle? So when a user presses home button - music will stop. When he will come back to app - music will be restored and this activity too (it is not MainActivity)
Thank you, guys
Here are the different LifeCycle states. Now to your answer,
#Override
protected void onStop() {
super.onStop();
backgroundmusic.pause();
length = backgroundmusic.getCurrentPosition();
}
#Override
protected void onResume() {
super.onResume();
backgroundmusic.seekTo(length);
backgroundmusic.start();
}
In public class MainActivity extends AppCompatActivity, It's the AppCompatActivity that is the main source of an Activity's functionality, hence in the above methods like super.onResume(); and super.onStop(); super refers to the AppCompatActivity class
assume that you can get the music service in the Application ,you may be looking for this :
public class MyApp extends Application{
MusicService musicService;
#Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
#Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
}
#Override
public void onActivityStarted(Activity activity) {
}
#Override
public void onActivityResumed(Activity activity) {
if(musicService==null) return;
if(!musicService.isPlaying()){
musicService.play();
}
}
#Override
public void onActivityPaused(Activity activity) {
if(musicService==null) return;
if(musicService.isPlaying()){
musicService.pause();
}
}
#Override
public void onActivityStopped(Activity activity) {
}
#Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
#Override
public void onActivityDestroyed(Activity activity) {
}
});
}
}
Hope this helps
I'm having trouble with resuming an app's activity after locking the device screen.
Here's the typical situation:
1. App is running.
2. Lock device.
3. Unlock device.
4. Instead of returning to running app, it shows a black screen.
I checked how the activity lifecycle is running and it seems this is what's happening when it unlocks:
1. Create
2. Start
3. Resume
4. Pause
Why is it stuck on pause? I'm sure this is really simple but as I'm still learning Android I'm super confused. Thanks for any help given!
EDIT:
I don't think I'm doing anything out of the ordinary but here's the basic code I'm using...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("Java", "Create");
// Initialize NDK audio properties here
// Initialize the Content View
setContentView(R.layout.activity_main);
// Setup toolbar
Toolbar myToolbar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(myToolbar);
// Start thread
thread = new Thread() {
public void run() {
setPriority(Thread.MAX_PRIORITY);
// Audio Thread is running here
}
};
thread.start();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
#Override
protected void onStart() {
super.onStart();
}
#Override
protected void onPause() {
super.onPause();
if (thread != null) {
try {
thread.join();
}
catch (InterruptedException e) {
// e.printStackTrace();
}
thread = null;
}
}
#Override
protected void onResume() {
super.onResume();
}
#Override
protected void onStop() {
super.onStop();
}
Try this (it will instruct your app to save state):
protected void onSaveInstanceState(Bundle icicle) {
icicle.putLong("param", 1);
super.onSaveInstanceState(icicle);
}
I am not thread expert. but here it seems you are blocking ui thread in onpause. thread.join causes the current thread(in this case ui thread) to pause execution until thread terminates.
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...