I am starting B Fragment from Fragment A.Now from Fragment B i hit Home Button.Again i open the app and it calls OnResume. Now if i hit back button it exit from the app.What should i do?
Fragment A to Fragment B Activity
Intent find = new Intent(getActivity(),FindActivityMain.class);
find.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
***startActivity(find);***Here i am not finishing the activity.
Fragment B Activity to Fragment B transaction code
Fragment myfindfragment = new FindFragmentMain(FindActivityMain.this,mylistitem,FindActivityMain.this,distance,featuredAD);
getSupportFragmentManager().beginTransaction()
.replace(R.id.blankfindlandingframe, myfindfragment).commitAllowingStateLoss();
}
Fragment A Activity is "SingleInstance" declared in Manifest.In General the Back Navigation works Perfect.But If i do these step ->
1.) Click Home Button
2.) Again long press home button to restore the app
3.) Click back button.Exit from the app.
This 3rd Step should not occur.
Please Help !!
remove find.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
add your code 'addToBackStack(null)'
Fragment myfindfragment = new FindFragmentMain(FindActivityMain.this,mylistitem,FindActivityMain.this,distance,featuredAD);
getSupportFragmentManager().beginTransaction()
.replace(R.id.blankfindlandingframe, myfindfragment).addToBackStack(null).commitAllowingStateLoss();
}
Inside Activity B add this:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
{
finish();
return false;
}
return super.onKeyDown(keyCode, event);
}
Related
I have a fragment which opens an activity (let's call it activity1). This activity (activity1) can open another activity (let's call it activity2). And again this activity (activity2) can open another activity (let's call it activity3). I would like to get the following behavior:
When pressing back in activity1 it will go back to the fragment - works.
When pressing back in activity2 it will go to activity1 - works.
When pressing back in activity3 it will go back to the fragment - does not work.
The last one does not work because it goes back to activity1. When moving from activity2 to activity3 I finish activity2. But how can I also finish activity1? I can't use the (Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK) flag like it was suggested in previous topics because I want it to go back to already opened fragment. How can I remove activity1 from the intent stack?
if you want the flexibility to have activity3 to show any activity when it finishes, you need to override onKeyDown and handle KEYCODE_BACK.
in Activity3.java
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
//show fragment or startActivity to show activity with fragment
return true;
}
}
return super.onKeyDown(keyCode, event);
}
if I launch a full-screen dialog like such
FragmentManager fragmentManager = getActivity().getFragmentManager();
DialogStyleCreator editor = new DialogStyleCreator();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.add(android.R.id.content, editor).commit();
How can I make it so that if I hit the android back arrow (in the top left of the menu) or the back button it closes the dialog instead of going back to the previous activity like in alert dialogs?
You may simply put your transaction in stack of fragment manager and override the function of back button
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (Integer.parseInt(android.os.Build.VERSION.SDK) > 5
&& keyCode == KeyEvent.KEYCODE_BACK
&& event.getRepeatCount() == 0) {
// Simply pop back your fragment stack here
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onBackPressed() {
// Simply pop back your fragment stack here
}
Since you have added the DialogFragment as transaction.add(android.R.id.content, editor).commit(); you may not have option to listen the dismiss call back.
I suggest you to create the DialogFragment instance and start it like
fm.beginTransaction().add(sampleDialog, "Dialog").commit();
and set the call back interface for Activity Back Button Click and call dismiss();
I am using Fragment A,Fragment B .. and so on. i want to navigate back to the previous Fragment, from the current Fragment.
I want to use Back Button in Mobile to get back navigation.
How can i do this ? OR i have to make Back button in layout xml.
i tried below code,
fTransaction.add(R.id.container, nF).addToBackStack(null).commit();
when i pressed Back Button in Mobile , it restarts the App.
To go back to previous fragment using phone's back button you should override onKeyDown() mehtod.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
{
getFragmentManager().popBackStack();
return true;
}
return false;
}
When replacing a fragment use replace instead of add and then addToBackStack:
fTransaction.replace(R.id.container, nF).addToBackStack(null).commit();
Try this:
fTransaction.addToBackStack(null).add(R.id.container, nF).commit();
I have 3 screens . 1->Fragment(inside fragmentactivity) 2->Fragment 3-> Activity. In screen 3 I call screen 1 to do something and I return again to screen 3. Now if I press back button in screen 3 I want to go to screen 2 instead of screen 1 . I tried this logic but it returned back to screen 1 instead of 2 .Any suggestions please?
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
Log.d("TAG","back button ");
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
if you want remove activity form Activity Stack you need call finish() method or start activity with Intent.FLAG_ACTIVITY_CLEAR_TOP, as second solution remove all activity from Stack you don't need that.
in your situation you need call finish() method on following step:
1-> 3
3-> 1
use addtobackstack in your fragment transaction:
getSupportFragmentManager().beginTransaction()
.add(detailFragment, "detail")
// Add this transaction to the back stack
.addToBackStack()
.commit();
http://developer.android.com/training/implementing-navigation/temporal.html
I want to send back my activity to starting one. Let me explain you.
I am using Main activity having 4 tabs, and each tabs has his own activity. Each activity in has more activities in it.
Problem starts when I click on 1st activity in my 1st tab. The next activity is independent. I create an intent of my next activity. In such case to prevent draw my tab-bar layout in that new activity. I use my existing activity layout, remove all the view in it. Then i put new activity in it.
e.g..
contentViewLayout.removeAllViews();
View view = activityManager.startActivity(id, intent).getDecorView();
contentViewLayout.addView(view, contentViewLayoutParams);
Its done. But problem arise when device BACK button pressed. Because new activity is starting in existing activity. So when back button pressed , then it quite to main activity and application close. I want to do that in such a case "new activity" should be terminate and existing activity must start again. And then if again BACK button pressed then it should terminate main activity.
I override the onKeyDown() down method as follow but it. dose not working properly?
In my new activity.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
childActivity = null;
contentViewLayout.removeAllViews();
mainActivity = new Intent(this,MainActivity.class);
View view = activityManager.startActivity(id, intent).getDecorView();
contentViewLayout.addView(view, contentViewLayoutParams);
return false;
}else{
return super.onKeyDown(keyCode, event);
}
}
But it always quite the application instead of closing child activity
I got it!
Actually the focused activity was the main activity... not the new activity. So all we have to do to set focused to our new activity. So in onCreate() method of new activity I put focused to one any component in new activit e.g..
button.setFocusable(true);
button.requestFocus();
So in such a case new activity onKeyDownButtonWorks
Blockquote
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0 ){
//My code goes here.
return true;
}
return super.onKeyDown(keyCode, event);
}
Take a look at this solution:
ActivityGroup not handling back key for ListActivity