Bottom nav view - one activity and two fragments - android

I'm trying to make a bottomNavigation that goes between the main activity, and two fragments. Right now I went after a tutorial and created a third fragment:
Here's the bottomNavigation listener:
private BottomNavigationView.OnNavigationItemSelectedListener bottom_navigation_listener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId())
{
case R.id.Piano:
setTitle("Pitch Piano");
PianoFragment fragment = new PianoFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction1 = getSupportFragmentManager().beginTransaction();
fragmentTransaction1.replace(R.id.frame, fragment, "Piano");
fragmentTransaction1.commit();
return true;
case R.id.Rhythm:
setTitle("Rhythm");
Rhythm fragment2 = new Rhythm();
android.support.v4.app.FragmentTransaction fragmentTransaction2 = getSupportFragmentManager().beginTransaction();
fragmentTransaction2.replace(R.id.frame, fragment2, "Rhythm");
fragmentTransaction2.commit();
return true;
case R.id.Intervals:
setTitle("Intervals and Chords");
Intervals_and_chords fragment3 = new Intervals_and_chords();
android.support.v4.app.FragmentTransaction fragmentTransaction3 = getSupportFragmentManager().beginTransaction();
fragmentTransaction3.replace(R.id.frame, fragment3, "Intervals");
fragmentTransaction3.commit();
return true;
}
return false;
}
};
Main Activity layout (shortened):
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.stefanawesome.piano.MainActivity">
<RelativeLayout
android:layout_width="581dp"
android:layout_height="503dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:background="#ffffff"
android:id="#+id/lin_lay"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.031"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintHorizontal_bias="0.5"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp">
<include
android:id="#+id/toolbar"
layout="#layout/tool_bar"
/>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/upperback"
android:layout_alignStart="#+id/upperback"
android:layout_below="#+id/myRectangleView"
app:itemBackground="#color/bottomNavView"
app:itemIconTint="#drawable/bottomnavbutton"
app:itemTextColor="#drawable/bottomnavbutton"
app:menu="#menu/bottom_nav_items" />
<FrameLayout
android:id="#+id/frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Rhythm Fragment layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.stefanawesome.piano.Rhythm">
<!-- TODO: Update blank fragment layout -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/toolbar"
layout="#layout/rhythmtoolbar"
/>
</RelativeLayout>
</FrameLayout>
Interval Fragment layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.stefanawesome.piano.Intervals_and_chords">
<!-- TODO: Update blank fragment layout -->
</FrameLayout>
Screenshot when the bottom nav bar is on the rhythm section:
Instead of jumping between the main activity, and then the two fragments, the main activity layout never goes away, even if you go to one of the fragments, the fragment layout is behind the main layout.
Also, if I added code in any of the fragments, the program crashes.

Related

Kotlin Fragment overlapping issue

I am using recyclerview. Then problem is when i click one of the row, new fragment should be open. It opens but, old fragment still appears. this is my screenshot:
As you see, when i click "opel" new fragment opens. But it is overlapping.
This is my fragment 1:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvarackategori"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/container">
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
This is my new fragment:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.aracYedekParca.chevrolet.ChevroletYedekParcaFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Chevrolet Yedek Parca" />
</FrameLayout>
this is my code:
override fun userItemClick(position: Int) {
//super.userItemClick(position) No need of it.
when (position) {
0 -> {
var fragmentOpel=OpelYedekParcaFragment()
var transaction = manager.beginTransaction()
transaction.replace(R.id.container,fragmentOpel)
transaction.commit()
Log.e("aki", "opel: " + position)
} //start a fragment
1 -> {
var fragmentChevrolet=ChevroletYedekParcaFragment()
var transaction = manager.beginTransaction()
transaction.replace(R.id.container,fragmentChevrolet)
transaction.commit()
Log.e("aki", "chevrolet: " + position)
} //start a fragment
Add a background to your new Fragment:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background:"#android:color/white"
tools:context=".ui.aracYedekParca.chevrolet.ChevroletYedekParcaFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Chevrolet Yedek Parca" />
</FrameLayout>
Basically that's how fragments work.
The methods name replace might be misleading, it doesn't replace the fragments it only lay a fragment in a layout.
so if you want the other fragment to be dismissed you have to remove it. otherwise just setting a background for your 2nd Fragment should workaround your issue

Android BottomNavigationView overlaps Recyclerview

I have a BottomNavigationView and both my fragments have a RecyclerView which content is partially hidden under the BottomNavigationView
Lots of people seem to have had this problem yet I tried all of the answers (make rootview LinearLayout and set height of FrameLayout to 0 etc...) and none of them work.
This is my activity_main.xml file
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager.widget.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="0dp" />
<FrameLayout
android:id="#+id/main_fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
android:foreground="?attr/selectableItemBackground"
app:itemTextColor="#color/color12"
app:itemIconTint="#color/color12"
app:menu="#menu/menu_sample_store">
</com.google.android.material.bottomnavigation.BottomNavigationView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
this is how I initialize it (MainActivity.java)
private void initHandlers()
{
this.adapter = new ViewPagerAdapter(getSupportFragmentManager(),this);
viewPager.setAdapter(adapter);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
int id = menuItem.getItemId();
switch (id)
{
case R.id.menu_option_local:
fragment = new AlphaListFragment(getApplicationContext());
break;
case R.id.menu_option_store:
fragment = new BetaListFragment(getApplicationContext());
break;
}
setActiveFragment(fragment);
return true;
}
});
}
private void setActiveFragment(Fragment fragment)
{
getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment_container, fragment).commit();
}
And if it might help, this is the xml file of a fragment
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerViewBeta"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
<ProgressBar
android:id="#+id/loadingBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_centerHorizontal="true" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
EDIT: SOLUTION FOUND
Make the rootview of activity_main a LinearLayout
Set its orientation to vertical
Set the layout_weigth of the FrameLayout to 1
Make sure that the FrameLayout has a height of 0dp

Fix botttom navigation bar keep expanding when its fragment contain Coordinator layout

I have bottom navigation bar activity with a fragment on top of it.
1 out of 3 menus that I have a fragment that uses Coordinator layout as its parent with App bar layout and collapsing toolbar. another menu works fine with Relative layout parent, but the fragment that has Coordinator layout doesn't work pretty well and make the bottom navigation bar expand its height by itself. Here I'm providing my Code and some screenshots. Thankyou
Here is the code of my fragment layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="#FFF"
app:layout_collapseMode="parallax">
<TextView
android:id="#+id/nama_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:layout_marginStart="10dp"
android:text="#string/nama_sample"
android:textSize="20dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/lokasi_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/lokasi_sample"
android:layout_alignStart="#id/nama_user"
android:layout_below="#id/nama_user"
android:layout_alignEnd="#id/foodie_user"
android:layout_marginTop="5dp"
android:drawableStart="#drawable/ic_location_on_black_24dp"
android:gravity="center"
android:textStyle=""/>
<TextView
android:id="#+id/foodie_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/foodie_sample"
android:layout_alignStart="#id/nama_user"
android:layout_below="#id/lokasi_user"
android:layout_marginTop="5dp"
android:drawableStart="#drawable/ic_foodie"
android:drawablePadding="5dp"
android:layout_marginBottom="20dp"
android:gravity="center"
android:textColor="#color/color_yellow"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignTop="#id/lokasi_user"
android:layout_alignBottom="#id/foodie_user"
android:layout_alignEnd="#id/nama_user"
android:gravity="center_horizontal">
<TextView
android:id="#+id/review_count_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="46"
android:textSize="25dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reviews"/>
</LinearLayout>
<ImageView
android:id="#+id/image_user"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="10dp"
android:src="#drawable/ic_person_black_24dp"
android:tint="#color/color_grey"
/>
</RelativeLayout>
</android.support.design.widget.CollapsingToolbarLayout>
<View
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#color/color_red" />
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior">
</android.support.v7.widget.RecyclerView>
</android.support.design.widget.CoordinatorLayout>
Here is the code of my Activity with a bottom navigation bar
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeActivity">
<FrameLayout
android:id="#+id/fragment_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/nav_view"
/>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#6DCCD1D8"
android:layout_above="#id/nav_view"/>
<android.support.design.widget.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
android:layout_alignParentBottom="true"
app:menu="#menu/bottom_nav_menu" />
</RelativeLayout>
and this is its activity java
public class HomeActivity extends AppCompatActivity implements
DiscoverFragment.OnFragmentInteractionListener,
SearchFragment.OnFragmentInteractionListener,
ProfileFragment.OnFragmentInteractionListener{
FrameLayout fragment;
final Fragment fragment1 = new DiscoverFragment();
final Fragment fragment2 = new SearchFragment();
final Fragment fragment3 = new ProfileFragment();
final FragmentManager fm = getSupportFragmentManager();
Fragment active = fragment1;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.discover_home:
fm.beginTransaction().hide(active).show(fragment1).commit();
active = fragment1;
return true;
case R.id.search_home:
fm.beginTransaction().hide(active).show(fragment2).commit();
active = fragment2;
return true;
case R.id.profile_home:
fm.beginTransaction().hide(active).show(fragment3).commit();
active = fragment3;
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
BottomNavigationView navView = findViewById(R.id.nav_view);
fragment = findViewById(R.id.fragment_holder);
navView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
fm.beginTransaction().add(R.id.fragment_holder, fragment3, "3").hide(fragment3).commit();
fm.beginTransaction().add(R.id.fragment_holder, fragment2, "2").hide(fragment2).commit();
fm.beginTransaction().add(R.id.fragment_holder,fragment1, "1").commit();
}
#Override
public void onFragmentInteraction(Uri uri) {
}
}
This is how my error looks like
Here is a screenshot of my error when I select profile tab
it pooped out when I choose profile bar
Thank you :)
please try removing this line from your XML
`android:fitsSystemWindows="true"
because it sets the padding of the View to ensure the contents don’t overlay the system windows.

BottomNavigationView Is not coming in the bottom after adding the fragment

I am a beginner in android , today i have created a BottomNavigationView activity , i want to show 3 different tabs with navigation buttons, so i created 3 fragments, the problem is after adding the fragments , the BottomNavigationView is showing in the top side, what should i do , if i want BottomNavigationView in the bottom as like it was before adding the fragments
here is my main activity code
package com.hackerinside.jaisonjoseph.polysocial;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView mTextMessage;
public BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
tab1 radio = new tab1();
android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.container, radio, radio.getTag()).commit();
case R.id.navigation_dashboard:
tab2 radio1 = new tab2();
android.support.v4.app.FragmentManager manager1 = getSupportFragmentManager();
manager1.beginTransaction().replace(R.id.container, radio1, radio1.getTag()).commit();
case R.id.navigation_notifications:
tab3 radio2 = new tab3();
android.support.v4.app.FragmentManager manager2 = getSupportFragmentManager();
manager2.beginTransaction().replace(R.id.container, radio2, radio2.getTag()).commit();
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tab1 radio = new tab1();
android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.container, radio, radio.getTag()).commit();
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
}
this is activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.hackerinside.jaisonjoseph.polysocial.MainActivity">
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:color/holo_blue_dark">
<TextView
android:id="#+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:text="#string/title_home" />
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="#menu/navigation" />
this is my first fragment tab1
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.hackerinside.jaisonjoseph.polysocial.tab1">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_blank_fragment" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
The BottomNavigationView does not appear automatically on the bottom of the view. You have to place them manually.
You can use a RelativeLayout for that.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.hackerinside.jaisonjoseph.polysocial.MainActivity">
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:color/holo_blue_dark">
<TextView
android:id="#+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:text="#string/title_home" />
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
android:layout_alignParentBottom="true"
app:menu="#menu/navigation" />
</RelativeLayout>
If changed your root LinearLayout to a RelativeLayout and added the parameter android:layout_alignParentBottom="true" to your BottomNavigationView. Hope that will help.
If you need more help, probably this link can help: https://medium.com/#hitherejoe/exploring-the-android-design-support-library-bottom-navigation-drawer-548de699e8e0
By the way you can also look at https://github.com/roughike/BottomBar
It's a custom view like new Material Design Bottom Navigation pattern (https://material.io/guidelines/components/bottom-navigation.html#bottom-navigation-behavior).

Android - Keeping the first fragment in backstack and don't re-create instance while replacing

I've developed a material NavigationDrawer which has some menus. Each menu is associated with individual fragments. I would like to keep my FragmentHome() always alive in backstack so that whenever I switch to different fragment and come back, the FragmentHome() should be there as before.
This is the xml where I'm creating the fragments,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="#layout/toolbar" />
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment
android:id="#+id/navigation_drawer"
android:name="NavigationDrawerFragment"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
This is how I'm creating and replacing the fragments,
#Override
public void onNavigationDrawerItemSelected(int position) {
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (position) {
case 0:
fragment = new FragmentHome();
title = getString(R.string.app_name);
break;
case 1:
fragment = new FragmentCategories();
title = getString(R.string.title_categories);
case 2:
fragment = new FragmentMyPlaylist();
title = getString(R.string.title_playlist);
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, fragment);
fragmentTransaction.commit();
// set the toolbar title
getSupportActionBar().setTitle(title);
}
}
The major problem I'm facing is, each fragment is created from the scratch whenever I'm replacing them. This is annoying and all of the components of the HomeFramgent() is re-created once again when they have already been created once.
UPDATE:
I made another solution for this issue which is to create individual framents in xml and replace them via the main container,
For this first I've created individual fragments that are associate with the fragment classes,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="#layout/toolbar" />
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment
android:id="#+id/FragmentHome"
android:name="FragmentHome"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment
android:id="#+id/FragmentCategories"
android:name="FragmentCategories"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment
android:id="#+id/FragmentMyPlaylist"
android:name="FragmentMyPlaylist"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment
android:id="#+id/navigation_drawer"
android:name="NavigationDrawerFragment"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
Then I'm declaring each fragment by id in the main activity,
FragmentManager fm = getSupportFragmentManager();
fragments[HOME] = fm.findFragmentById(R.id.FragmentHome);
fragments[CATEGORIES] = fm.findFragmentById(R.id.FragmentCategories);
fragments[PLAYLIST] = fm.findFragmentById(R.id.FragmentMyPlaylist);
FragmentTransaction transaction = fm.beginTransaction();
for (int i = fragments.length - 1; i > 0; i--) {
transaction.hide(fragments[i]);
}
transaction.commit();
Now whenver I want to replace a frament I'm doing this inside onNavigationDrawerItemSelected(),
fragmentManager.beginTransaction().replace(R.id.container, new
FragmentHome()).commit();
This solves the issue of re-creating each fragments and is created once after the application runs. Again this gives me a restriction of not refreshing the other fragments. All fragments are created at once and not re-creating on change, which I don't want.
I'm looking for an efficient way so that only the FragmentHome() will stay in backstace and all other fragments will get refreshed while replacing.
Any kind of help would be greatly appreciated...
Use FragmentManager.saveFragmentInstanceState to capture the state of your HomeFragment before it is replaced. You will need to declare a variable in the activity that hosts your fragments to save the result. When the HomeFragment is reselected, use Fragment.setInitialSavedState to restore the saved state of HomeFragment before adding it to the FragmentManager.

Categories

Resources