私はアンドロイドに新しいです。私は5つのフラグメントを持つアクティビティを持っていました。私はaddTobackStackを使って、戻るボタンをクリックすると前のフラグメントに移動できます。それは仕事をしますが、私は、戻るボタンが押されたときにフラグメントを置き換えていた関数を呼び出す必要があります。 助けてください?フラグメントをナビゲートする
ここにコードがあります。
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>
[FragmentManager.OnBackStackChangedListener](https://developer.android.com/reference/android/app/FragmentManager.OnBackStackChangedListener.html)を実装して、現在どのフラグメントがバックスタックにあるかに応じてメソッドを呼び出すことができます。私が理解するように、現在、メソッドはボタンのクリックのようなタッチ操作を介して呼び出されます。バックスタックの変更でこれを行うことはできず、代わりの方法を見つけ出す必要があります。あなたがもっと助けが必要な場合は教えてください。 –
このFragmentManager.OnBackStackChangedListenerを実装する場所と、どのフラグメントがバックスタックにあるかを確認する方法。 –
はい正確にこれらのメソッドは、画像ビューでクリックすると呼び出されます –