问题
I'm currently using Fragment with Backstack. In one of the fragment e.g "Menu" I wanted to prompt an alertdialog when user tapped on back button. But I'm having some problems in achieving that. I listed two scenario below that I tried.
Scenario 1:
I added a KeyListener in the "Menu" fragment that will trigger when user tapped on back button.
I add another fragment on top of "Menu" called "Report". When I pressed back button in "Report" fragment it will still prompt out the alertDialog that used in "Menu" fragment. The keyListener is still active in the background.
Menu Fragment
rootView.setFocusableInTouchMode(true);
rootView.requestFocus();
rootView.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction()!=KeyEvent.ACTION_DOWN)
return true;
switch (keyCode) {
case KeyEvent.KEYCODE_BACK :
Log.i("Menu", "onKey Back listener is working!!!");
new AlertDialog.Builder(getActivity())
.setTitle("Information")
.setMessage("Do you wish to exit from Menu Screen?")
.setNegativeButton("No", null)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
((MainActivity)getActivity()).selectItem(0, true);
}
}).create().show();
break;
}
return true;
}
});
Scenario 2:
Use onBackPressed method on the MainActivity class. This will prompt out an alertDialog in "Menu" fragment. But the moment it prompts out the alertDialog, this fragment will automatically end despite the user is still yet to select any option of the alertDialog. This may due to this code getFragmentManager().addOnBackStackChangedListener(getListener());
. But I can't remove this code as I need it for other fragments.
MainActivity.class
@Override
public void onBackPressed() {
FragmentManager manager = getFragmentManager();
if (manager != null) {
int backStackEntryCount = manager.getBackStackEntryCount();
Log.i(TAG, "backstackcount :"+backStackEntryCount);
FragmentManager fragmentManager = getFragmentManager();
if(getFragmentManager().getBackStackEntryCount()-1 >=0){
Fragment currentFragment = fragmentManager
.findFragmentByTag(fragmentManager.getBackStackEntryAt(getFragmentManager().getBackStackEntryCount()-1).getName());
Log.i(TAG, "class: "+currentFragment.getClass());
Log.i(TAG, "backstackCount: "+backStackEntryCount);
globalCurrentFragment = currentFragment;
if(currentFragment.getClass() == Master_Menu.class){
Log.i("currentFragment", "getClass = true");
new AlertDialog.Builder(MainActivity.this)
.setTitle("Information")
.setMessage("Do you wish to exit from Master Account session?")
.setNegativeButton("No", null)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
if (globalCurrentFragment != null) {
globalCurrentFragment.onResume();
}
}
}).create().show();
}
else{
if (globalCurrentFragment != null) {
globalCurrentFragment.onResume();
}
}
//if offer list
}else{
globalCurrentFragment = null;
selectItem(0, false);
}
}
super.onBackPressed();
}
Once again, I wanted to prompt an alertDialog only at "Menu" fragment and do not have any affect on other fragment. Please do advice regarding on this method. If there is any other information needed please do let me know.
回答1:
I have solved this issue with the following code. Set keyListener at onResume and remove the keylistener when switching to other fragment.
public void setKeyListenerOnView(View v){
Log.i(TAG, "setKeyListenerOnView");
v.setFocusableInTouchMode(true);
v.requestFocus();
v.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction()!=KeyEvent.ACTION_DOWN)
return true;
switch (keyCode) {
case KeyEvent.KEYCODE_BACK :
Log.i("Menu", "onKey Back listener is working!!!");
new AlertDialog.Builder(getActivity())
.setTitle("Information")
.setMessage("Do you wish to exit from Master Account session?")
.setNegativeButton("No", null)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
((NavigationDrawer)getActivity()).selectItem(0, true);
}
}).create().show();
break;
}
return true;
}
});
}
@Override
public void onResume() {
super.onResume();
Log.i(TAG, "onResume");
setKeyListenerOnView(getView());
}
//Remove KeyListener. Implement this when changing to other fragment.
rootView.setFocusableInTouchMode(true);
rootView.requestFocus();
rootView.setOnKeyListener(null);
来源:https://stackoverflow.com/questions/27011930/android-fragment-onbackpressed-display-alertdialog