I have a small issue in my android app. I want to kill the current activity when the back button is pressed. I have included this in my code:
public override void OnBackPressed()
{
this.Finish();
base.OnBackPressed();
}
But the activity doesn't finish when I go back to the previous page. I have an external callback method in this activity that is called even when I'm not on this activity. The method get's called as many times as I open the activity so I figured the activity is not getting closed properly. For example the method (SignalR callback) doesn't get called when I don't open the activity but as I open and close it, the method keeps getting called the number of times I opened and closed the activity when ever there a function callback.
Does anyone have any idea why this is happening?
You can unsubscribe your callback in onStop()
Related
My app get minimized when I press the back button on the home fragment, which is fine, but when I open it back up it shows me a blank activity. I guess it's not loading the fragment that it's supposed to load.
I added a text view with some text on the activity but it's not showing me that so I'm seeing something else.
How should I manage this issue?
Your activity has a lifecycle. If a lifecycle state is changing, then a method will be called. If you open your app, after you minimize your application, then the method "onResume()" will be called. Try to use this method in your activity to check, whether your activity is the right activity.
UPDATE(Thanks to Santiago Gil):
It's necessary to call finish() on the onboarding activity.
I used toasts and found that the app was returning to the launcher activity, but it was not going through the onCreate so it appeared blank, I now call finish() on the onboarding activity and so far it's working fine. Thanks
I am testing my android app that is in development.
It contains a single MainActivity
I noticed through testing that onDestroy was being called every time I push the back button.
I thought that this was weird. So I created a fresh empty activity app using android studio, and added no code. Just a simple hello world.
Even in this hello world app, onDestroy is being called everytime I press back.
I am running a Samsung S4 and I have not reason to believe that it is resource starved. What is going on here?
I tried setting android:launchMode to all available values in AndroidManifest.xml, and none of that worked....
Activity getting destroyed whenever you are pressing back button i.e android default behavior. This is how code flows.
override onBackPressed inside your Activity
/**
* called when user press back button on device
*/
#Override
public void onBackPressed() {
super.onBackPressed();
}
Go inside onBackPressed which lies inside FragmentActivity which shows that it will first pop all fragments from activity and then it will finish activity.
/**
* Take care of popping the fragment back stack or finishing the activity
* as appropriate.
*/
public void onBackPressed() {
if (!mFragments.getSupportFragmentManager().popBackStackImmediate()) {
supportFinishAfterTransition();
}
}
It is normal and expected behavior that OnDestroy() is called after the back button is pressed. This is a standard part of the Android Activity Lifecycle. You can read about the lifecycle here: https://developer.android.com/guide/components/activities/activity-lifecycle.html
I would not recommend overriding the back button behavior as jitesh has suggested unless you have good reason. Users will expect that your app is "closed" (destroyed) once the back button is pressed.
If you want OnDestroy() not being called everytime you tap back button:
#Override
public void onBackPressed() {
// super.onBackPressed(); // remove this line
}
I have a list view in the previous intent and when I tap on that it go to the next activity. Now when I press the back button or up button it comes back to the previous activity. But when I tap the list item again it give me run time Unexpectedly app closed error. Please help me.
It's hard to answer without code, but maybe it's because you don't finish your next activity when you press the back button
It's because of Activity life cycle...
When you go to the next activity then current activity is paused for you. when you press the back or up button then your previous activity resume.
That's why you have to define your code in onResume() method also where you got stuck. because when you come back onResume method is called
You can try override onBackPressed method in your activities :
#Override
public void onBackPressed() {
// put a breakpoint here and check your listview/activities states
//super.onBackPressed();
}
onCreate() does not get called when I leave the application using back button and start it immediately. I believe this is because, Android has not killed the application process yet. I tried using #AfterViews, the same happens. How could I make sure that every time the application is started, my specific code runs?
Does not get called when I leave the application using back button and then start.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
methodToRun();
}
I use this onBackPressed() to exit the application.
#Override
public void onBackPressed() {
this.moveTaskToBack(true);
this.finish();
}
Even #AfterViews does not get called when I leave the application using back button and then start.
#AfterViews
void checkAgreementFlag(){
methodToRun();
}
I want methodToRun() to always get called. How would I do it?
Call methodToRun() from onResume() of activity.
onResume(): Called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it.
onCreate(): is called when your application is starting first time.
onRestart(): Called after your activity has been stopped, prior to it being started again.
onStart(): Called when the activity is becoming visible to the user.
onStop() : When the activity is no longer visible.
For the back key scenario activity lifecycle:
onCreate()->onStart()->onResume()->Activity running
After pressing back key
onPause()->onStop()
If the activity is coming back then
onRestart()->onStart()->onResume()->Activity running
otherwise onDestroy() will be called.
I want to know How an activity is visible once i press home button and relaunch the activity
for eg. If an application went in background and lauch again then MyActicity.java should be visible and not the same screen on which I quit.
Please suggest the possible solution
Thanks
Monali
As you can see in the documentation the onPause() method is called, whenever your Activity is paused. Just put this code in your Activity code:
public void onPause() {
this.finish();
}
If you do so, the Activity will be closed, whenever it is paused.
I don't know if I understood well your questions but if you have 2 activities A and B. if you are in B and press home button and then when you return to the app you want to go to A.. you have to call finish() method inside onPause() method on Activity B.