を使用して、それぞれの断片に私は3つのトップレベルの宛先(宛先ごとに1つのフラグメント)と下部のナビゲーションを実装しています。 Searchフラグメントには、検索ボタンがありました。これは、クリックすると、ActivityXという別のアクティビティに移動します。上のボタンを使用してActivityXからナビゲートすると、Searchの代わりにHomeにナビゲートされます。上のボタンをSearchに移動します。それを達成する方法?は移動戻るボタン
マイMainActivity:ActivityXで
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp=getSharedPreferences("login_details",MODE_PRIVATE);
if(!sp.getBoolean("login_status", false)){
isLoggedIn=false;
}
else
isLoggedIn=true;
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
BottomNavigationView bottomNav = (BottomNavigationView) findViewById(R.id.navigation);
fragmentManager=getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft1=fragmentManager.beginTransaction();
ft1.add(R.id.container,new HomeFragment());
ft1.commit();
updateToolbarText("Home");
bottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
if(item.getItemId()==R.id.action_home){
fragmentManager=getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft1=fragmentManager.beginTransaction();
ft1.replace(R.id.container,new HomeFragment());
ft1.commit();
updateToolbarText("Home");
}
if(item.getItemId()==R.id.action_search){
fragmentManager=getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft2=fragmentManager.beginTransaction();
ft2.replace(R.id.container,new SearchFragment());
ft2.commit();
MainActivity.super.onBackPressed();
updateToolbarText("Search");
}
if(item.getItemId()==R.id.action_settings){
fragmentManager=getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft3=fragmentManager.beginTransaction();
ft3.replace(R.id.container,new SettingsFragment());
ft3.commit();
updateToolbarText("Settings");
}
return true;
}
});
}
が(onBackpressedされています)ボタンをアクションバーを検出しましたか?解決策は私のために働いていません。私がこれを使用した後、アプリケーションがクラッシュしています。 – Rajendra
私はちょうどあなたの問題に似ている私の例を示していた、少しそれをあなたのニーズに合っているようにそれを配置します。私の場合、私は2つの活動をしていました。 MainActivityには2つのフラグメントがあります。各フラグメントから2番目のアクティビティに行くことができますが、戻ってみたいときは、情報を渡します。 – cariss
あなたの返信ありがとう。私は何とかインテントを使用して解決策を得られなかったし、何が間違っていたのか分からなかった。しかし、私はあなたのソリューションからアイデアを得た。 – Rajendra