与儿童片段的片段backstack

时间:2017-03-28 14:15:34

标签: java android android-fragments back-stack

我的应用程序有一个带抽屉的主要活动。抽屉上的每个项目都会将我发送给一个片段。

当我按下这些片段的后退按钮时,我想进入主要活动。

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

实际上我和不同的父母需要同样的事情。

我该怎么做?

这是我的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;
}
}

食谱片段:

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

1 个答案:

答案 0 :(得分:1)

我的解决方案是覆盖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();

            }
        }

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

示例:

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