Adnan Sheikh
Adnan Sheikh

Reputation: 57

Navigating fragments

I'm new to the android. I've an activity with 5 fragments and I've used addTobackStack so that I can move to the previous fragments when back button is clicked. It does the work but I want to call the functions in which I've been replacing the fragments when back button is pressed. Any help?

Here is the code.

Dashboard.java

package com.example.sheikhspc.design;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.Fragment;
import android.content.Intent;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class dashboard extends AppCompatActivity {

ImageView home1, subs1, noti1,settings1,date1;
Fragment frag = null;
TextView home, subs,noti,set,date;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard);
    home1 = (ImageView) findViewById(R.id.home1);
    subs1 = (ImageView) findViewById(R.id.subs1);
    noti1 = (ImageView) findViewById(R.id.noti1);
    settings1 = (ImageView) findViewById(R.id.settings1);
    date1 = (ImageView) findViewById(R.id.date1);
    frag = new dashboard1();
    home = (TextView)findViewById(R.id.hometv);
    subs = (TextView)findViewById(R.id.substv);
    noti = (TextView)findViewById(R.id.notitv);
    set = (TextView)findViewById(R.id.settingstv);
    date = (TextView)findViewById(R.id.datestv);

    FragmentManager fm = getFragmentManager();
    fm.beginTransaction().add(R.id.container1,frag).commit();

    Toolbar mytoolbar = (Toolbar)findViewById(R.id.mytoolbar);

    setSupportActionBar(mytoolbar);
    getSupportActionBar().setTitle("Home");
    home.setTextColor(0xFF000000);









}


public void dbfragment(View view)
{
    if(frag != null)
    {


          home1.setBackgroundDrawable(getResources().getDrawable(R.drawable.home_icon_black)  ome1.setBackgroundDrawable(getResources().getDrawable(R.drawable.home_icon_black) );
home.setTextColor(0xFF000000);
          subs1.setBackgroundDrawable(getResources().getDrawable(R.drawable.subscription_ic on));
           noti1.setBackgroundDrawable(getResources().getDrawable(R.drawable.noti_icon));
        settings1.setBackgroundDrawable(getResources().getDrawable(R.drawable.setting_icon));


         date1.setBackgroundDrawable(getResources().getDrawable(R.drawable.date_icon));
        date1.setBackgroundDrawable(getResources().getDrawable(R.drawable.d))    
        frag = new dashboard1();
        getSupportActionBar().setTitle("Home");
        FragmentManager fm = getFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();

        fragmentTransaction.replace(R.id.container1,frag);

        fragmentTransaction.commit();




    }
}
public void dates(View view)
{
    if(frag != null)
    {
        home1.setBackgroundDrawable(getResources().getDrawable(R.drawable.home_icon));
        subs1.setBackgroundDrawable(getResources().getDrawable(R.drawable.subscription_icon));
        noti1.setBackgroundDrawable(getResources().getDrawable(R.drawable.noti_icon));
        settings1.setBackgroundDrawable(getResources().getDrawable(R.drawable.setting_icon));
        date1.setBackgroundDrawable(getResources().getDrawable(R.drawable.date_icon_black));
        date.setTextColor(0xFF000000);
        getSupportActionBar().setTitle("Available Dates");
        frag = new AvailableDates();
        FragmentManager fm = getFragmentManager();

        FragmentTransaction fragmentTransaction = fm.beginTransaction();

        fragmentTransaction.replace(R.id.container1,frag);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }
}

public void subscription(View view)
{
    if (frag != null)
    {
        home1.setBackgroundDrawable(getResources().getDrawable(R.drawable.home_icon));
        subs1.setBackgroundDrawable(getResources().getDrawable(R.drawable.subscription_icon_black));
        noti1.setBackgroundDrawable(getResources().getDrawable(R.drawable.noti_icon));
        settings1.setBackgroundDrawable(getResources().getDrawable(R.drawable.setting_icon));
        date1.setBackgroundDrawable(getResources().getDrawable(R.drawable.date_icon));
        subs.setTextColor(0xFF000000);
        getSupportActionBar().setTitle("Subscriptions");
        frag = new Subscription();
        FragmentManager fm = getFragmentManager();

        FragmentTransaction fragmentTransaction = fm.beginTransaction();

        fragmentTransaction.replace(R.id.container1,frag);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }
}
public void noti(View view)
{
    getSupportActionBar().setTitle("Notifications");
    if (frag != null)
    {
        home1.setBackgroundDrawable(getResources().getDrawable(R.drawable.home_icon));
        subs1.setBackgroundDrawable(getResources().getDrawable(R.drawable.subscription_icon));
        noti1.setBackgroundDrawable(getResources().getDrawable(R.drawable.noti_icon_black));
        settings1.setBackgroundDrawable(getResources().getDrawable(R.drawable.setting_icon));
        noti.setTextColor(0xFF000000);
        date1.setBackgroundDrawable(getResources().getDrawable(R.drawable.date_icon));
        frag = new Notification();
        FragmentManager fm = getFragmentManager();

        FragmentTransaction fragmentTransaction = fm.beginTransaction();

        fragmentTransaction.replace(R.id.container1,frag);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }
}
public void settings(View view)
{
    getSupportActionBar().setTitle("Settings");
    if (frag != null)
    {
        set.setTextColor(0xFF000000);
        home1.setBackgroundDrawable(getResources().getDrawable(R.drawable.home_icon));
        subs1.setBackgroundDrawable(getResources().getDrawable(R.drawable.subscription_icon));
        noti1.setBackgroundDrawable(getResources().getDrawable(R.drawable.noti_icon));
        settings1.setBackgroundDrawable(getResources().getDrawable(R.drawable.setting_ion_black));
        date1.setBackgroundDrawable(getResources().getDrawable(R.drawable.date_icon));
        frag = new Settings();
        FragmentManager fm = getFragmentManager();

        FragmentTransaction fragmentTransaction = fm.beginTransaction();

        fragmentTransaction.replace(R.id.container1,frag);
        fragmentTransaction.addToBackStack(null);

        fragmentTransaction.commit();
    }
}
public void signout(View view)
{
        Intent intent = new Intent(this, Login.class);
        startActivity(intent);

}

@Override
public void onBackPressed() {

    if (getFragmentManager().getBackStackEntryCount() > 0 ){
        getFragmentManager().popBackStack();
    } else {
        super.onBackPressed();
    }


}
}

Activity_dashboard.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_dashboard"
android:layout_width="match_parent"
android:layout_height="match_parent"


android:background="@color/white"
tools:context="com.example.sheikhspc.design.dashboard">

<include layout="@layout/toolbar"
    android:id="@+id/mytoolbar"/>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background"
    android:layout_below="@id/mytoolbar">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/headrl"
        android:paddingTop="5dp"
        android:paddingRight="20dp"
        android:paddingLeft="20dp"
        android:paddingBottom="5dp"
        android:background="@color/white"
        tools:ignore="NotSibling">


<!--            <Button
            android:text=""
            android:background="@drawable/subscription_icon_black"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/notificationbtn"
            android:onClick="subscription"
            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/adbtn"
            android:layout_toEndOf="@+id/adbtn"
            android:layout_marginTop="7dp" />-->
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/home_icon_black"
            android:onClick="dbfragment"
            android:id="@+id/home1"

            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Home"
            android:layout_below="@+id/home1"
            android:id="@+id/hometv"

            android:textSize="10dp"
            android:layout_marginTop="2dp"/>


        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/date_icon"
            android:onClick="dates"
            android:id="@+id/date1"
            android:layout_marginLeft="30dp"

            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/home1"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Dates"
            android:layout_below="@+id/date1"
            android:textSize="10dp"
            android:id="@+id/datestv"
            android:layout_marginLeft="30dp"
            android:layout_toRightOf="@+id/home1"
            android:layout_marginTop="0dp"/>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/noti_icon"
            android:onClick="noti"
            android:id="@+id/noti1"
            android:layout_marginLeft="35dp"

            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/date1"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Notifications"
            android:layout_below="@+id/noti1"
            android:textSize="10dp"
            android:id="@+id/notitv"
            android:layout_marginLeft="15dp"
            android:layout_toRightOf="@+id/date1"
            android:layout_marginTop="0dp"/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/subscription_icon"
            android:onClick="subscription"
            android:id="@+id/subs1"
            android:layout_marginLeft="35dp"

            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/noti1"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Subscription"
            android:layout_below="@+id/noti1"
            android:textSize="10dp"
            android:layout_marginLeft="25dp"
            android:id="@+id/substv"
            android:layout_toRightOf="@+id/noti1"
            android:layout_marginTop="0dp"/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/setting_icon"
            android:onClick="settings"
            android:id="@+id/settings1"
            android:layout_marginLeft="35dp"

            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/subs1"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Settings"
            android:layout_below="@+id/noti1"
            android:textSize="10dp"
            android:id="@+id/settingstv"
            android:layout_marginLeft="30dp"
            android:layout_toRightOf="@+id/subs1"
            android:layout_marginTop="0dp"/>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/logout_icon"
            android:onClick="signout"
            android:id="@+id/signout1"
            android:layout_marginLeft="35dp"

            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/settings1"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Logout"
            android:layout_below="@+id/noti1"
            android:textSize="10dp"
            android:layout_marginLeft="30dp"
            android:layout_toRightOf="@+id/settings1"
            android:layout_marginTop="0dp"/>


        <!--<Button
            android:text=""
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/adbtn"
            android:onClick="dates"

            android:layout_marginTop="7dp"
            android:layout_marginLeft="10dp"
            android:background="@drawable/date_icon_black"
            android:layout_toRightOf="@+id/dbbtn" />

        <Button
            android:text=""
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/dbbtn"
            android:layout_marginTop="7dp"

            android:background="@drawable/home_icon_black"

            android:onClick="dbfragment"
            android:layout_marginLeft="10dp"

            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />-->

    </RelativeLayout>
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/headrl"
        >
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/Black"
            android:id="@+id/container1">


        </RelativeLayout>

    </ScrollView>


</RelativeLayout>

Upvotes: 0

Views: 82

Answers (1)

Ishita Sinha
Ishita Sinha

Reputation: 2294

You can implement the FragmentManager.OnBackStackChangedListener and call your methods there depending upon which fragment is currently in the back stack. As I understand, currently the methods are called via touch interactions like button clicks. You wouldn't be able to do this on backstack change and need to figure out an alternate way.

You need to implement OnBackStackChangedListener in the activity. Better do the following in onCreate in your dashboard.java:

fragmentManager = getFragmentManager();
fragmentManager.addOnBackStackChangedListener(this);

Then override onBackStackChanged in your dashboard.java like this:

@Override
public void onBackStackChanged() {
    //get the current fragment being displayed
    Fragment fragment = fragmentManager.findFragmentById(R.id.container1);
    if (fragment instanceof AvailableDates) {
        //do your stuff
    } else if (fragment instanceof Subscription) {
        //do other stuff
    } //and so on for each fragment
 }

You can make different implementations for your onClick methods like thus:

public void dates(View view)
{
    if(frag != null)
    {
      openAvailableDates();
     }
}

and then implement openAvailableDates thus:

void openAvailableDates() {
    home1.setBackgroundDrawable(getResources().getDrawable(R.drawable.home_icon));
    subs1.setBackgroundDrawable(getResources().getDrawable(R.drawable.subscription_icon));
    noti1.setBackgroundDrawable(getResources().getDrawable(R.drawable.noti_icon));
    settings1.setBackgroundDrawable(getResources().getDrawable(R.drawable.setting_icon));
    date1.setBackgroundDrawable(getResources().getDrawable(R.drawable.date_icon_black));
    date.setTextColor(0xFF000000);
    getSupportActionBar().setTitle("Available Dates");
    frag = new AvailableDates();
    FragmentManager fm = getFragmentManager();

    FragmentTransaction fragmentTransaction = fm.beginTransaction();

    fragmentTransaction.replace(R.id.container1,frag);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}

and so on for your other methods and then call these new methods from onBackStackChanged.

One more tip. It's not good practice to name your classes starting with a lowercase letter. It's best to refactor dashboard.java to Dashboard.java.

Let me know if you need more help.

Upvotes: 1

Related Questions