Java 如何应用片段中按下的后退按钮
我是android新手。我不能用反压法。我有一个Activity类,它有四个片段,比如A、B、C、D。当我午餐时,默认情况下,片段A是活动的,并且片段A上有移动另一个片段的链接。当我从片段A中移动另一个片段,如B,C,D,并按下后退按钮时,它会返回到片段A,如果我从片段A中按下后退按钮,它会显示一个对话框。 我使用onBackPressed()如下所示Java 如何应用片段中按下的后退按钮,java,android,android-fragments,onbackpressed,Java,Android,Android Fragments,Onbackpressed,我是android新手。我不能用反压法。我有一个Activity类,它有四个片段,比如A、B、C、D。当我午餐时,默认情况下,片段A是活动的,并且片段A上有移动另一个片段的链接。当我从片段A中移动另一个片段,如B,C,D,并按下后退按钮时,它会返回到片段A,如果我从片段A中按下后退按钮,它会显示一个对话框。 我使用onBackPressed()如下所示 public void onBackPressed() { AlertDialog.Builder alertDialogBuilder
public void onBackPressed() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
// set title
alertDialogBuilder.setTitle("Exit");
// set dialog message
AlertDialog.Builder builder = alertDialogBuilder
.setMessage("Do you really want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, close
// current activity
MainActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
但是它对所有片段都有效,我只想对片段A有效,你必须在活动的onBackPressed only中处理这些需求。我通常采用以下方法: 我保留一个已定义所有片段的枚举和一个用于跟踪当前片段的参数:
enum FRAGMENTS{
fragmentA, fragmentB, fragmentC, fragmentD
}
FRAGMENTS mCurrentFragment;
现在,更改片段的逻辑应该有:
public void changeFragment(FRAGMENTS newFragment){
//Your logic
mCurrentFragment = newFragment;
}
最后,逻辑被反压:
onBackPressed{
if(mCurrentFragment == FRAGMENTS.fragmentA){
//Your code here of asking the user if he/she really wants to quit
super.onBackPressed();
}else{
changeFragment(getPreviousFragment(mCurrentFragment));
}
}
如果不想使用enum,可以使用final int或任何其他字符串值来表示不同的片段,并使用mCurrentFragment参数来跟踪显示的currentFragment,然后可以轻松地使用活动的onBackPressed方法中的代码
如果您需要更多说明,请告诉我。您必须仅在活动的onBackPressed中处理此类要求。我通常采用以下方法: 我保留一个已定义所有片段的枚举和一个用于跟踪当前片段的参数:
enum FRAGMENTS{
fragmentA, fragmentB, fragmentC, fragmentD
}
FRAGMENTS mCurrentFragment;
现在,更改片段的逻辑应该有:
public void changeFragment(FRAGMENTS newFragment){
//Your logic
mCurrentFragment = newFragment;
}
最后,逻辑被反压:
onBackPressed{
if(mCurrentFragment == FRAGMENTS.fragmentA){
//Your code here of asking the user if he/she really wants to quit
super.onBackPressed();
}else{
changeFragment(getPreviousFragment(mCurrentFragment));
}
}
如果不想使用enum,可以使用final int或任何其他字符串值来表示不同的片段,并使用mCurrentFragment参数来跟踪显示的currentFragment,然后可以轻松地使用活动的onBackPressed方法中的代码
如果您需要更多说明,请告诉我。在替换时将片段添加到
backStack
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack if needed
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
然后重写活动中的
onBackPressed
方法在替换时将片段添加到backStack
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack if needed
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
然后重写活动中的onBackPressed方法,下面是您要查找的答案:) 方法1 //如果你面临任何问题,请告诉我 方法2或在您的情况下,只需将此方法粘贴到您的主要活动中,这样您也可以实现您想要的目标:)
这就是你想要的答案:) 方法1 //如果你面临任何问题,请告诉我 方法2或在您的情况下,只需将此方法粘贴到您的主要活动中,这样您也可以实现您想要的目标:)
使用下面的代码,希望对您有所帮助
//Always replace/add your fragment with a tag so that you can use that tag in future
Fragment fr = getSupportFragmentManager().findFragmentByTag("Fragment_Name");//Fragment Name is a tag to identify fragment
if (fr == null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_container, new Demo_Fragment(), "Fragment_Name").commit();
}
//Now override onBackPressed method in MainActivity Only no need to do it in Fragment Class
@Override
public void onBackPressed() {
Fragment fr = getSupportFragmentManager().findFragmentByTag("FragmentA");
if(fr==null)
//replace FragmentA
else
//Show Alert Box
}
使用下面的代码,希望对您有所帮助
//Always replace/add your fragment with a tag so that you can use that tag in future
Fragment fr = getSupportFragmentManager().findFragmentByTag("Fragment_Name");//Fragment Name is a tag to identify fragment
if (fr == null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_container, new Demo_Fragment(), "Fragment_Name").commit();
}
//Now override onBackPressed method in MainActivity Only no need to do it in Fragment Class
@Override
public void onBackPressed() {
Fragment fr = getSupportFragmentManager().findFragmentByTag("FragmentA");
if(fr==null)
//replace FragmentA
else
//Show Alert Box
}
为此,您需要执行以下操作 在保存当前片段对象的BaseFragment或活动上创建方法
public void setCurrentFragment(Fragment currentFragment) {
this.currentFragment = currentFragment;
}
并创建一个返回当前片段对象的方法
public Fragment getCurrentFragment() {
return currentFragment;
}
现在只需要调用getCurrentFragment
方法并检查片段是否是片段的实例
if (getCurrentFragment() instanceof AFragment) {
showAlert
} else {
super.onBackPressed()
}
为此,您需要执行以下操作 在保存当前片段对象的BaseFragment或活动上创建方法
public void setCurrentFragment(Fragment currentFragment) {
this.currentFragment = currentFragment;
}
并创建一个返回当前片段对象的方法
public Fragment getCurrentFragment() {
return currentFragment;
}
现在只需要调用getCurrentFragment
方法并检查片段是否是片段的实例
if (getCurrentFragment() instanceof AFragment) {
showAlert
} else {
super.onBackPressed()
}
如果要重写onBack pressed方法,请将此代码添加到in-onActivityCreated()方法中
如果要重写onBack pressed方法,请将此代码添加到in-onActivityCreated()方法中
你有办法知道什么时候片段a是显示的吗?是的,什么时候片段a是显示的吗?检查我的答案,让我知道你克服了还是没有你有办法知道什么时候片段a是显示的吗?是的,什么时候片段a是显示的吗?检查我的答案,让我知道你克服了还是没有你真的认为你的答案会解决Shailesh的问题吗因为他只需要一个警报,而且只有当片段A不显示其他时,您的代码才会对所有片段创建警报。是的,在这种情况下,方法1对shailesh很有效,方法2对所有片段都有效,返回按钮为track,但如果我们在backbutton为track时传递一些条件,方法2也更可取。方法1不会我猜也是为他工作。因为在任何片段上单击“后退”按钮都会使“如果”条件始终为“KEYCODE\u后退”。不,如果他将方法1放在片段A中,它会起作用,因为方法1我们可以放在任何特定片段中,我们可以跟踪特定片段的点击。你不能覆盖
片段中的onKeyDown
方法。你知道吗?您只能在活动中覆盖,这就像onBackPressed
方法,你不能在Fragment
中覆盖它,你真的认为你的答案会解决Shailesh问题吗,因为他只想要一个警报,而且只有当片段A不显示其他,你的代码会对所有片段创建警报。是的,在这种情况下,方法1对Shailesh和方法2适用于所有片段返回按钮为轨迹,但如果我们在返回按钮为轨迹时传递一些条件,方法2也更可取。我想方法1也不适用于他。因为在任何片段上单击“后退”按钮都会使“如果”条件始终为“KEYCODE\u后退”。不,如果他将方法1放在片段A中,它会起作用,因为方法1我们可以放在任何特定片段中,我们可以跟踪特定片段的点击。你不能覆盖片段中的onKeyDown
方法。你知道吗?您只能在活动
中重写,这类似于onBackPressed
方法,您不能在片段