Android findFragmentById返回null
我有一个带有导航抽屉和空布局的活动。在创建活动时,我显示片段(在活动的空布局中)。片段的id为Android findFragmentById返回null,android,android-fragments,onbackpressed,Android,Android Fragments,Onbackpressed,我有一个带有导航抽屉和空布局的活动。在创建活动时,我显示片段(在活动的空布局中)。片段的id为dateVehicleMainContent。选择导航项时,我会显示其他片段。当按下back时,它返回到上一个片段 当当前片段是我显示的第一个片段时,我想显示一个注销对话框(dateVehicleMainContent)。当为每个碎片按back键时,将显示该图标。我不希望它显示在占据活动空白布局的其他片段中。所以我使用findframentbyid。可能这不起作用,因为我在活动的空布局中显示了片段 Da
dateVehicleMainContent
。选择导航项时,我会显示其他片段。当按下back时,它返回到上一个片段
当当前片段是我显示的第一个片段时,我想显示一个注销对话框(dateVehicleMainContent
)。当为每个碎片按back键时,将显示该图标。我不希望它显示在占据活动空白布局的其他片段中。所以我使用findframentbyid
。可能这不起作用,因为我在活动的空布局中显示了片段
DateTimePicker是片段类
这是一个活动
,布局为空,并使用onBackPressed()
检查当前片段是否是id为dateVehicleMainContent
@Override
public void onBackPressed() {
Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.dateVehicleMainContent);
if (currentFragment instanceof DateTimePicker) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(DateVehiclePicker.this);
alertDialog.setTitle("Logout");
// Setting Dialog Message
alertDialog.setMessage("Do you want to Logout?");
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
sessionManager.logoutUser();
}
});
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
dialog.cancel();
}
});alertDialog.show();
} else {
super.onBackPressed();
}
}
活动的布局:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.prematixsofs.taxiapp.DateVehiclePicker">
<!-- The main content view -->
<RelativeLayout
android:id="@+id/mainContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<RelativeLayout
android:id="@+id/drawerPane"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start">
<!-- Profile Box -->
<RelativeLayout
android:id="@+id/profileBox"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#ff02c7c4"
android:padding="8dp">
<ImageView
android:id="@+id/avatar"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="15dp"
android:src="@drawable/user" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="42dp"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@+id/avatar"
android:orientation="vertical">
<TextView
android:id="@+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:textColor="#fff"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/viewProfile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginTop="4dp"
android:text="View Profile"
android:textColor="#fff"
android:textSize="12sp" />
</LinearLayout>
</RelativeLayout>
<!-- List of Actions (pages) -->
<ListView
android:id="@+id/navList"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_below="@+id/profileBox"
android:background="#ffffffff"
android:choiceMode="singleChoice" />
</RelativeLayout>
这是片段布局的一部分..显示此片段时,我想显示注销警报对话框:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/dateVehicleMainContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.looper.loop.DateTimePicker">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dateVehicleLinearLayoutMainContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
好的,让我试试这样的。这不是一个非常完整的示例,也不是可构建的,因为它使用了我的一些自定义代码(类),但它很好地演示了该方法
String title = getIntent().getStringExtra(EXTRA_TITLE);
tag = getIntent().getStringExtra(EXTRA_FRAGMENT_TAG);
FilterFragment currentFragment = null;
if (tag.equals(FragmentMyWishlist.TAG)) {
currentFragment = (FilterFragment) getSupportFragmentManager().findFragmentByTag(FragmentMyWishlist.TAG);
} else if (tag.equals(FragmentMyLibrary.TAG)) {
currentFragment = (FilterFragment) getSupportFragmentManager().findFragmentByTag(FragmentMyLibrary.TAG);
}
您可能不必使用getSupportFragmentManager()
,也可以使用getFragmentManager()
完成工作,但想法是一样的。我不得不使用SupportFragmentManager,因为我正在使用import android.support.v4.app.Fragment代码>
编辑:
FragmentMyWishlist
有一个public静态字符串TAG=“FragmentMyWishlist”代码>
FragmentMyLibrary
有一个公共静态字符串TAG=“FragmentMyLibrary”
尝试改用findFragmentByTag()
,并确保所有片段都有标记:)此外,通过YourFragment.onDetach()
的回调通知父活动以显示对话框。@Shark嗨..我是android新手。你能给我一个例子的链接或者一个例子的答案吗?我在附近贴了一些代码来说明这个方法。如果这听起来很愚蠢的话,很抱歉。什么是FragmentMyWishlist.TAG
。它是您在如下方式替换片段时设置的标记:fragmentTransaction.addToBackStack(null).replace(R.id.mainContent,new DateTimePicker(),“tags”).commit()代码>?我尝试使用包含标记的字符串进行检查,就像您使用MyFragment.TAG
一样,但标记无法解析。我做对了吗?你把绳子放在哪里了?标记的值是多少。它在引发此活动的活动中设置,从而更改这些片段。。。tag
的值从启动活动的Intent
中获取,然后活动知道将日期路由到哪个Fragment
。