![](/img/trans.png)
[英]How to navigate to previous fragment using back-button within NavigationDrawer?
[英]Navigate via back-button from child fragment back to parent activity
我有一个主要活动和2个片段。 在我的主要活动中,我有一个导航抽屉,可以浏览片段。 我像这样处理后退按钮:
@Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStackImmediate();
} else {
super.onBackPressed();
}
}
在两个片段之间导航后,当我按下“后退”按钮时,我从后堆栈中获得了最后一个片段。 但是,不可能从片段导航到另一个片段。
更清晰:
我有一个活动(带有导航抽屉)和2个片段。 从导航抽屉中,我可以随意浏览两个片段。 但是,当我单击“后退”按钮时,我总是希望返回到main_activity.xml
。
当您浏览“活动”时,最好的方法是使用这样的Intent
(如果我理解您的问题):
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
**OnCreate() :-**
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);
this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
ActionBar mActionBar = getActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.actionbar_category_feed_list, null);
RelativeLayout rrBack = (RelativeLayout) mCustomView.findViewById(R.id.rr_back);
TextView title = (TextView) mCustomView.findViewById(R.id.txt_title);
title.setText("");
title.setTypeface(typeFaceMedium);
rrBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mTracker.send(new HitBuilders.EventBuilder().setCategory("" + userUsername).setAction("On Search Feed Screen").setLabel("Pressed Back Button").build());
Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);
finish();
}
});
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
**actionbar_category_feed_list.xml**
?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="@android:color/white" >
<RelativeLayout
android:id="@+id/rr_back"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" >
<ImageView
android:id="@+id/img_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:focusable="false"
android:src="@drawable/back_red" />
</RelativeLayout>
<com.converza.library.CustomTextView
android:id="@+id/txt_title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/rr_back"
android:gravity="center_vertical"
android:paddingLeft="8dp"
android:text="@string/app_name" />
</RelativeLayout>
**At last in your fragment Activity use a OnactivityResult() for get a Returnintent from Activity.**
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Const.FEEDDETAIL && resultCode == Activity.RESULT_OK) {
Log.e(tag, "DatashBoard Const.FEEDDETAIL RESULT_OK");
} else if (requestCode == Const.FEEDDETAIL && resultCode == Activity.RESULT_CANCELED) {
Log.e(tag, "DatashBoard Const.FEEDDETAIL RESULT_CANCELED");
}
}
您必须按标签查找片段,并检查其是否为null,如果为null,则替换该片段,或者如果片段被隐藏,则再次显示该片段。
@Override
public void onBackPressed() {
Fragment fr = getSupportFragmentManager()
.findFragmentByTag("Fragment_Tag");
if (fr == null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.container,
new Main_Fragment(), "Fragment_Tag").commit();
} else {
finish();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.