android - findFragmentById returns null -


i have activity navigation drawer , empty layout. on create of activity, display fragment(in empty layout of activity). fragment's id datevehiclemaincontent. on selecting navigation items display other fragments. when pressed returns previous fragment.

i want display logout dialog when current fragment first fragment displayed(datevehiclemaincontent). displayed when pressed every fragment. don't want displayed in other fragments occupy empty layout of activity. use findfragmentbyid. maybe not working because display fragment in empty layout of activity.

datetimepicker fragment class.

this activity empty layout , onbackpressed() check if current fragment fragment 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 want 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();     } } 

the activity's layout:

<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"> <!-- main content view --> <relativelayout     android:id="@+id/maincontent"     android:layout_width="match_parent"     android:layout_height="match_parent" />   <!-- 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> 

this part of fragment's layout ..when fragment displayed want show alert dialog logout:

<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"> 

ok, lemme try this. isn't complete example, nor it's buildable because uses of custom code (classes) demonstrates approach well.

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);         } 

you may not have use getsupportfragmentmanager() , can job done getfragmentmanager() idea same. had use supportfragmentmanager because i'm using import android.support.v4.app.fragment;

edit:

fragmentmywishlist has public static string tag = "fragmentmywishlist"; fragmentmylibrary has public static string tag = "fragmentmylibrary";


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -