2016-09-28 6 views
0

Ich bin neu auf dem Android. Ich habe eine Aktivität mit 5 Fragmenten und ich habe addTobackStack verwendet, sodass ich zu den vorherigen Fragmenten wechseln kann, wenn auf die Schaltfläche Zurück geklickt wird. Es macht die Arbeit, aber ich möchte die Funktionen aufrufen, in denen ich die Fragmente ersetzt habe, wenn die Zurück-Taste gedrückt wurde. Irgendwelche Hilfe?Navigieren Fragmente

Hier ist der 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> 

+0

Sie können den [FragmentManager.OnBackStackChangedListener] (https://developer.android.com/reference/android/app/FragmentManager.OnBackStackChangedListener.html) implementieren und Ihre Methoden dort aufrufen, je nachdem, welches Fragment gerade im Backstack ist . Soweit ich weiß, werden die Methoden derzeit über Touch-Interaktionen wie Knopfklicks aufgerufen. Sie wären nicht in der Lage, dies bei Backstack-Änderung zu tun und müssen einen alternativen Weg finden. Lass es mich wissen, wenn du mehr Hilfe brauchst. –

+0

Wo kann ich diesen FragmentManager.OnBackStackChangedListener implementieren und wie ich überprüfen kann, welches Fragment im Backstack ist. –

+0

Ja genau diese Methoden werden aufgerufen, wenn Sie auf eine Bildansicht klicken –

Antwort

1

können Sie die FragmentManager.OnBackStackChangedListener implementieren und rufen Sie dort Ihre Methoden in Abhängigkeit von dem Fragment ist zur Zeit in den Rücken Stapel. Soweit ich weiß, werden die Methoden derzeit über Touch-Interaktionen wie Knopfklicks aufgerufen. Sie wären nicht in der Lage, dies bei Backstack-Änderung zu tun und müssen einen alternativen Weg finden.

Sie müssen in der Aktivität OnBackStackChangedListener implementieren. Besser Sie wie folgt in onCreate in Ihrem dashboard.java:

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

Dann onBackStackChanged in Ihrem dashboard.java wie dies außer Kraft setzen:

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

@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 
} 

Sie verschiedene Implementierungen für Ihre onClick Methoden wie so machen kann

und dann implementieren openAvailableDates also:

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

und so weiter für Ihre anderen Methoden und rufen Sie dann diese neuen Methoden von onBackStackChanged.

Noch ein Tipp. Es ist nicht ratsam, Ihre Klassen mit einem Kleinbuchstaben zu benennen. Es ist am besten, dashboard.java zu Dashboard.java zu refaktorieren.

Lassen Sie mich wissen, wenn Sie weitere Hilfe benötigen.

+0

Danke Ishita. Ich arbeite daran. Eine weitere Frage Wie kann ich die Methode in der if-Bedingung aufrufen, da jede Methode einen View-Parameter hat. –

+0

Ich habe gesucht, aber nichts bekommen –

+0

Das ist genau das, was ich meinte, als ich sagte, dass du einen alternativen Weg finden musst. Wenn Sie die Ansicht in der Methode überhaupt nicht verwenden müssen, könnten Sie eine "Null" übergeben, nehme ich an. Aber ich weiß nicht, ob das ratsam wäre. Versuchen Sie es auf jeden Fall einmal und sehen Sie. –