Manage toolbar’s navigation and back button from fragment in android

Add a toolbar to your xml <android.support.v7.widget.Toolbar android:id=”@+id/toolbar” android:layout_width=”match_parent” android:layout_height=”?attr/actionBarSize” android:background=”?attr/colorPrimary” android:theme=”@style/ThemeOverlay.AppCompat.ActionBar” app:popupTheme=”@style/ThemeOverlay.AppCompat.Light”> <TextView android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”Fragment title”/> </android.support.v7.widget.Toolbar> Then inside your onCreateView method in the Fragment: Toolbar toolbar = view.findViewById(R.id.toolbar); toolbar.setNavigationIcon(R.drawable.ic_back_button); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getActivity().onBackPressed(); } });

Hide/Show Action Bar Option Menu Item for different fragments

Try this… You don’t need to override onCreateOptionsMenu() in your Fragment class again. Menu items visibility can be changed by overriding onPrepareOptionsMenu() method available in Fragment class. @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); } @Override public void onPrepareOptionsMenu(Menu menu) { menu.findItem(R.id.action_search).setVisible(false); super.onPrepareOptionsMenu(menu); }

FragmentPagerAdapter getItem is not called

KISS Answer: Simple use FragmentStatePagerAdapter instead of FragmentPagerAdapter. I got the answer.. Firstly I thought to delete this question as I am doing a very silly mistake but this answer will help someone who is facing the same problem that Instead of FragmentPagerAdapter, use FragmentStatePagerAdapter. As @BlackHatSamurai mentioned in the comment: The reason this works … Read more

Default Activity not found in Android Studio

Have you added ACTION_MAIN intent filter to your main activity? If you don’t add this, then android won’t know which activity to launch as the main activity. ex: <intent-filter> <action android:name=”android.intent.action.MAIN”/> <action android:name=”com.package.name.MyActivity”/> <category android:name=”android.intent.category.LAUNCHER” /> </intent-filter>

Android – Activity vs FragmentActivity? [duplicate]

ianhanniballake is right. You can get all the functionality of Activity from FragmentActivity. In fact, FragmentActivity has more functionality. Using FragmentActivity you can easily build tab and swap format. For each tab you can use different Fragment (Fragments are reusable). So for any FragmentActivity you can reuse the same Fragment. Still you can use Activity … Read more

TabHost with Fragments and FragmentActivity

I would suggest creating a separate fragment file for each tab. I recently did this as well, so I have outlined my code below: Layout Files activity_main.xml <android.support.v4.app.FragmentTabHost xmlns:android=”http://schemas.android.com/apk/res/android” android:id=”@android:id/tabhost” android:layout_width=”match_parent” android:layout_height=”match_parent”> <LinearLayout android:orientation=”vertical” android:layout_width=”match_parent” android:layout_height=”match_parent”> <TabWidget android:id=”@android:id/tabs” android:orientation=”horizontal” android:layout_width=”match_parent” android:layout_height=”wrap_content” android:layout_weight=”0″/> <FrameLayout android:id=”@android:id/tabcontent” android:layout_width=”0dp” android:layout_height=”0dp” android:layout_weight=”0″/> <FrameLayout android:id=”@+id/realtabcontent” android:layout_width=”match_parent” android:layout_height=”0dp” android:layout_weight=”1″/> </LinearLayout> </android.support.v4.app.FragmentTabHost> tab1_view.xml … Read more