gpt4 book ai didi

java - 带有子 fragment 的 fragment backstack

转载 作者:行者123 更新时间:2023-12-02 13:34:33 25 4
gpt4 key购买 nike

我的应用程序有一个带抽屉的主要 Activity 。抽屉里的每一件元素都会把我带到一个 fragment 。

当我按下这些 fragment 的后退按钮时,我想转到主要 Activity 。

问题是我的一个 fragment 中有一个子 fragment 。我希望子级中的后退按钮将我带到父级 fragment 。

实际上,我对不同的 parent 需要同样的东西。

我该怎么做?

这是我的MainActivity:

package com.example.matancohen.sg50;

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog;
import android.text.Html;
import android.view.Gravity;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.v4.app.Fragment;
import android.view.MenuItem;
import android.widget.Toast;



public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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.addDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);

displayView(R.id.nav_home);



}


@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}

}

private void displayView(int itemId) {
Fragment fragment = null;

switch (itemId) {
case R.id.nav_home:
fragment = new Article();
break;
case R.id.nav_Recipes:
fragment = new RecipesMain();
break;
case R.id.nav_Converter:
fragment = new Converter();
break;
case R.id.nav_Videos:
fragment = new Videos();
break;
case R.id.nav_Tracking:
fragment = new Tracking();
break;
case R.id.nav_TrainerMenu:
fragment = new TrainerMenu();
break;
case R.id.nav_Settings:
fragment = new Settings();
break;
case R.id.nav_About:
showDialog();
break;

}


if (fragment != null) {
final Fragment finalFragment = fragment;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
getSupportFragmentManager()
.beginTransaction()
.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out)
.replace(R.id.content_main, finalFragment)
.commit();
}
}, 0);
}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}




@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {

displayView(item.getItemId());
return true;
}
}

食谱 fragment :

package com.example.matancohen.sg50;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.support.v4.app.FragmentActivity;


public class RecipesMain extends Fragment implements View.OnClickListener {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_recipes,container,false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//you can set the title for your toolbar here for different fragments different titles
getActivity().setTitle("מתכונים");
ImageButton stuffedBreast = (ImageButton)getView().findViewById(R.id.stuffedbreast);
stuffedBreast.setOnClickListener(this);
ImageButton breastSweetpotato = (ImageButton)getView().findViewById(R.id.chickensweetpotato);
breastSweetpotato.setOnClickListener(this);
ImageButton Cabbage = (ImageButton)getView().findViewById(R.id.cabbage);
Cabbage.setOnClickListener(this);
ImageButton chickenMeatballs = (ImageButton)getView().findViewById(R.id.chickenmeatballs);
chickenMeatballs.setOnClickListener(this);
ImageButton Sushi = (ImageButton)getView().findViewById(R.id.sushi);
Sushi.setOnClickListener(this);
ImageButton SintaBalls = (ImageButton)getView().findViewById(R.id.sintaballs);
SintaBalls.setOnClickListener(this);
ImageButton Maakuda = (ImageButton)getView().findViewById(R.id.maakuda);
Maakuda.setOnClickListener(this);
}


@Override
public void onClick(View v) {
Fragment fragment = null;


switch (v.getId()) {

case R.id.chickenmeatballs:
fragment = new RecipeChickenMeatballs();
break;

case R.id.chickensweetpotato:
fragment = new RecipeChickenSweetPotato();
break;

case R.id.cabbage:
fragment = new RecipeCabbage();
break;

case R.id.stuffedbreast:
fragment = new RecipeStuffedBreast();
break;

case R.id.sushi:
fragment = new RecipeSushi();
break;

case R.id.sintaballs:
fragment = new RecipeSintaballs();
break;

case R.id.maakuda:
fragment = new RecipeMaakuda();
break;
}
if (fragment != null){
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.bottomtop, R.anim.topbottom,R.anim.popbottomtop,R.anim.poptopbottom);
fragmentTransaction.replace(R.id.content_main, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}

最佳答案

我的解决方案是重写 MainActivity 中的 onBackPressed() 方法并编写此代码,

    @Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
return;
}

if (getSupportFragmentManager().findFragmentById(R.id.fragment_container) instanceof Article) {
((Article) getSupportFragmentManager().findFragmentById(R.id.fragment_container)).handleOnBackPress();
return;
}

if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
displayView(R.id.nav_home);
} else {
super.onBackPressed();

}
}

并在您的文章 fragment 中添加一个名为handleOnBackPress()的新方法,并编写用于处理后退按钮的自定义代码。

示例:

public void handleOnBackPress() {
System.exit(0);
}

关于java - 带有子 fragment 的 fragment backstack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43071998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com