I have 2 activities with fragments inside of them. Whenever I press the back button, the fragment gets destroyed (Used FragmentTransaction's replace method for adding them into the activity) instead of going back to the first activity immediately.
How can I achieve the behavior I want to have?
You can override onBackPressed method in your activity and check if the fragmentManager.getBackStackEntryCount()==0. If yes finish() the activity.
Hope it helps.
Related
I launch main activity, change some settings in UI, press back button and then reopen the activity. onCreate() is called again and activity is back to default state. Why is this?
I would expect only onResume() to be called since I have this in Manifest:
android:launchMode="singleInstance"
Try using moveTaskToBack(true); in onBackPressed() to make your current activity hide instead of destroying, so you can reopen it again
#Override
public void onBackPresses(){
moveTaskToBack(true);
}
I launch main activity, change some settings in UI, press back button and then reopen the activity. onCreate() is called again and activity is back to default state. Why is this?
This is expected behavior of activity life cycle. When you press back button then activity get destroyed, it will start from onCreate method.
This is because when you press back, it destroyes the activity.
I think that what you want is to keep the state when you come back to this activity, not really to have one instance of it. So you should save the information you need and then restore them in onCreate.
Single instance only means (if I remember it well) that if you launch multiple activities without finishing them, the ones that have singleInstance will go back to front when you will call startActivity() and will not call onCreate. But this means you can't have this behavior by pressing back.
Maybe you can override onBackPressed and start another activity (the one you should go back to) instead of calling super.onBackPressed(). this should do what you want but if think it will be kind of difficult to manage.
By the way remember that Activities might be killed by the system itself, so you should not rely on the fact that your activity as not been killed
I have a scenario where I have 2 fragments.
Clicking on a button in the first fragment takes you to the 2nd fragment.
By clicking the "UP" button in the 2nd fragment you'll get navigated back to the first fragment. Unfortunately the OnCreateView() method of the first fragment is not called.
Is there a way to call it? Which methods are called by clicking the "up" button?
It won't be called since 1st fragment is not yet detached from its activity and not destroyed yet. In your case, onResume() callback would be the best place to put your code.
OnCreateView doesnt get called because the fragment A is already created.
Read about the Fragmetn/Activity lifecycle and u will understand that.
OnResume will get called once pressed back from Fragment B, so u can put your logic in that method.
I'm trying to give an alert when leaving a fragment by different ways, and user can choose to stay in the current fragment. Is that possible?
Like there is an activity A and two fragment F1 & F2. Now you are in F2, backbutton lead to F1 and some menu item in A also lead to F1, how I write code in one place to stop both of these two events?
onstop is invoked when fragment stopping, how can I stop this stopping proccess in the onstop method?
Depending on what you are trying to achieve, you could override the following methods:
onDestroy
onStop
onPause
onResume
onBackPressed
In addition to the lifecycle methods mentioned by dustedrob, you should also read up on onDestroyView and onDetach to see if those suit your needs.
You can use onBackpressed() of an activity that is attach to all fragment.
Show an alert on Onbackpressed().
I'd like to destroy an Activity when the user clicks on a button (not the back button, a different button). I've decided to just call super.onBackPressed. Is that okay? What's a better way to close the current Activity without closing the whole app?
for closing an Activity you can use finish() , but you have to aware of back stack, if you finish your last activity in the stack so there will be no activity in your stack and you must restart your app .
see more information on : Task and BackStack
The default implementation of super.onBackPressed finishes the activity.
Instead of using super.onBackPressed , Better way to close the current activity will be to call finish() method.
Also, When you press the back button, the onResume method is called.
Use onResume Method and refresh your stuff there.
Also, if it is listView refresh the data -> https://stackoverflow.com/a/12662994/2006412
I am totally new to android development....what piece of code shall I write to move back to the last activity from the current activity on a button click?
I use intents to switch between the activities but is there anything specific to resume back on the last activity?
Please advise...
Thanks,
Pressing the back button should do that "out of the box"...
If the user clicks on Back button, application will automatically take you to previous Activity. You don't need to do anything.
If what you pretend is to implement that action from current Activity, for example through a Back menu option, just call finish() in your Activity. You can call setResult before that, if you called that activity with startActivityForResult
Ger
By default back resume previous activity.
You should read this to better understand android Activities:
http://developer.android.com/guide/topics/fundamentals.html#lcycles