I used below code to close activity after back button pressed.
#Override
public void onBackPressed()
{
super.onBackPressed();
finish();
}
after 5 time pressed it worked
how to solve it ?
remove super.onBackPressed(); to avoid handling the back button by the class parents
If you want to finish when back press. don't need to Override.
#Override
public void onBackPressed()
{
// do something
}
this will do nothing when you press back button.
onBackPressed() usually needed when you want make custom ask to close dialog when back press.
Related
I have two Activities A and B.
Activity A has a Tablayout with some Tabs. When I navigate from A to B I use this:
Intent intent = new Intent(A, B.class);
A.startActivity(intent);
When I now navigate back from B to A I have a question:
1) When using Android's back button, the selected tab / scrolling position from A was remembered
2) When using an Intent or NavUtils.navigateUpFromSameTask(this); then the selected tab and scroll Position is NOT remembered but set to initial value
Can someone explain me what is going on here?
1) when navigation from activity A to B, the android system does not destroy activity A, but takes it to the back stack and adds B to the foreground. thats why when you press the back button or call onBackPressed() from the java code activity B is destroyed and A is set to the foreground. here is an example from the docs : Understand Tasks and Back stack
2) when using an intent/navigateUpFromSameTask activity A is recreated and set to the foreground and B is set to the background, it's like adding another activity A to the stack so it will be A,B,A but if you press the back btn then you will be back to B and then A.
if you want to keep the scroll position and other data in activity A you call the onBackPressed in B or use the onSaveInstanceState to save the data and use it in the onCreate .
here is an example of saved instance:
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putString("VariableName", variableData);
savedInstanceState.putString("VariableName", variableData);
savedInstanceState.putString("VariableName", variableData);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.penguin_main);
if(savedInstanceState!=null){
bookData = (String) savedInstanceState.getSerializable("VariableName");
bookData = (String) savedInstanceState.getSerializable("VariableName");
bookData = (String) savedInstanceState.getSerializable("VariableName");
}
}
You can set the current scroll position and tab position in activity A's on overriding onSaveInstance(Bundle savedInstanceState) method. When return to activity you can get onRestoreInstanceState(Bundle savedInstanceState) to restore it.
Hope it helps :)
Because NavUtils.navigateUpFromSameTask() just calls startActivity() and if android:launchMode="standard" the activity will be instantiated and created again and that is why can not remember the previous selected tab. To solve this issue you can override onNavigateUp() and inside that setCurrentItem(index) the index of tab you want to be displayed.
#Override
public boolean onNavigateUp() {
myViewPager.setCurrentItem(position, true);
return true;
}
Edit
You can use another solution to solve the problem by setting android:launchMode="singleTop" on activity but this solution may not applicable in all the application.
When you start an Activity, the first page will be opened! But when the back button is pressed, it navigates between the saved state of the activityTo simulate the back button pressed, you can try this:
#Override
public void onBackPressed() {
finish(); //your choice, thought not needed as super.onBackPressed(); is called if nothing is assigned here
}
or on toolbar back button clicked click:
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
In my Application, i'm transitioning to a DetailsFragment when the user clicks on a list item. and there are TWO options to be back at the main Fragment (the List Fragment).
Press the back button. (no problem here, because I handle this in onBackPressed() in MainActivity)
Press the Toolbar back arrow (Here is my problem).
When the user presses the toolbar back arrow, I call the following
getActivity().getSupportFragmentManager().popBackStack();
how can I intercept this event in MainActivity?
(There are some manipulations I am doing in MainActivity when the List Fragment is visible to the user.
Just put this code
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
I have activity AActivity which calls Activity BActivity.
In the AndroidManifest I specify B as follows:
<activity android:name=".main.BActivity"
android:parentActivityName=".main.AActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".main.AActivity" />
</activity>
If I'm in Activity B and click on the phone's Back-Button, then I go back to Activity A without calling A.onCreate(); again - that's how I want it to be. But If I'm in Activity B and click the ActionBar's Back button on the top left, then I go back to Activity A again, but A.onCreate(); is called. How can I use the backbutton of the top of the Activity to behave in the manner the phone's back button does, i.e. not calling StartingActivity.onCreate() ?
Btw: Activity A and B are extending from AppCompatActivity.
Inside your OnClick(View view) function call finish().
Related guide
you can use android:launchMode="singleTop" in manifest inside activity A. read this following link for more details return parent activity correctly
Call finish() onBackPressed() and onOptionsItemSelected() when id == android.R.id.home
#Override
public void onBackPressed(){
if(EcologDriverAIDApplication.DEBUG){
Log.i(TAG, "onBackPressed");
}
finish();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//Back button pressed
if (id == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
How can I use the backbutton of the top of the Activity to behave in the manner the phone's back button does, i.e. not calling StartingActivity.onCreate() ?
//declare android:onClick="backButtonPressed" in your xml in the back button layout
public void backButtonPressed(View view) {
onBackPressed();
}
Why it should work:
When you press the bottom back button of android, onBackPressed() is called. As you have said, the bottom back button tap works the way you want it to. Therefore, just call it from your click listener of the back button at the action bar
I have a mainActivity with viewPager and three tabs, each tab has its respective fragment. i want when this mainActivity is launched and the backButton is pressed, nothing to happen.
to achieve this, i override onBackPressed() inside the mainActivity the one has viewPager with tabs, but when the mainActivity ia launched one of the tabs, which represents Fragment, by default appears, and when i press the backButton, i go back, which means the Backbuton is not disabled.
that is it, in the mainActivity, i have:
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
Log.w(TAG, "#onBackPressed().");
}
and he backbutton is not disabled in the Fragment, each time i press it, the logCat displays the message in the onBackPressed and i go back, and i do not want this, i want while i am in the Fragment "any one of the tabs", the backButton should has no effect.
please let me know how to achieve this?
Try not calling
super.onBackPressed();
Although the log will still print because the method will be called, this will stop Android performing it's logic and going back.
If you want the fragment tabs to do nothing, but the viewpager to handle it, do something like this
#Override
public void onBackPressed() {
if (getCurrentTab == 0) {
super.onBackPressed();
} else {
//This will stop the fragment tabs from doing anything when back is pressed
}
}
If you need avoid the back button, comment the super call //super.onBackPressed();
In a Fragment i have this:
onPause(){
super.onPause();
if(flag){
getActivity.finish();
}else{
}
}
onResume(){
flag = true;
super.onResume();
}
and there is a Button, on click i set this flag to false:
Button.onClick{
flag = false;
}
The idea is:
when the button is clicked don't finish this Activity. But when device BackButton is pressed finish().
But this logic isn't working. The reason is, when i press BackButton of the device, onPause is not being called.
This is the 4th Fragment in the Activty. So when i press BackButton i can see the 3rd Fragment. Which i don't want to.
I am using this on a API 10 device, But my application uses support library v4.
Thank You.
If you want to close the activity on the back press, then Override the onBackPressed inside the activity as following:
public void onBackPressed() {
Fragment fragment = getSupportFragmentManager().findFragmentByTag("yourfragment");
if (fragment instanceOf YourFragmet) {
finish();
return;
}
super.onBackPressed();
}
If you want to close the activity on the button click, then you can call
Button.onClick{
getActivity().onBackPressed();
}
OR
Button.onClick{
getActivity().finish();
}
If When you are transitioning between Fragments, you call addToBackStack() as part of your FragmentTransaction, then the back button will take you to the fragment on the top of the back stack
Why can't you use onDetach() of Fragment? On Back key pressed, there will be onDetach() called on the fragment to remove that.
#Override
public void onDetach() {
super.onDetach();
if(flag){
getActivity.finish();
}else{
}
}