片段卡住 - 无法转到上一个片段

时间:2015-11-10 13:11:51

标签: android android-fragments

我有一个片段(A),其中正在打开另一个片段(B)。当我按下后退按钮时,它只是刷新片段(B)而不是退出它并返回片段A.

我尝试在Activity的onBackPressed回调方法中弹回片段A,但它没有改变一件事:

@Override
public void onBackPressed() {
   if(B.active)
   {
       mFragmentManager.popBackStack( A.TAG , 0);
       B.active = false;
   }
}

** active布尔值只是我作为解决方案的一部分添加的内容。一旦片段被实例化,它就被初始化为TRUE。

1 个答案:

答案 0 :(得分:0)

我不知道如何投入项目!

package com.example.a;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by 77930 on 2015/11/9.
 */
public class ActivityA extends AppCompatActivity{
    @InjectView(R.id.c)
    LinearLayout c;
    @InjectView(R.id.btn)
    TextView btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.c);
        ButterKnife.inject(ActivityA.this);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                fragmentTransaction.add(R.id.e, FragA.newInstance(), "A");
//        fragmentTransaction.addToBackStack("A");
                fragmentTransaction.commitAllowingStateLoss();
            }
        });

    }
}

package com.example.a;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by 77930 on 2015/11/10.
 */
public class FragA extends Fragment{
    private View rootView;
    public static FragA newInstance() {
        return new FragA();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    public FragA() {
    }
    @InjectView(R.id.a)
    LinearLayout a;
    @InjectView(R.id.btn)
    TextView btn;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fraga,container,false);
        ButterKnife.inject(this,rootView);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.e, FragB.newInstance(), "B");
                fragmentTransaction.addToBackStack("B");
                fragmentTransaction.commitAllowingStateLoss();
            }
        });

        return rootView;
    }
}


package com.example.a;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by 77930 on 2015/11/10.
 */
public class FragB extends Fragment{
    private View rootView;
    public static FragB newInstance() {
        return new FragB();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    public FragB() {
    }
    @InjectView(R.id.b)
    LinearLayout b;
    @InjectView(R.id.btn)
    TextView btn;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragb,container,false);
        ButterKnife.inject(this, rootView);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().getSupportFragmentManager().popBackStackImmediate();
            }
        });

        return rootView;
    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:id="@+id/c"
    android:background="#4b14b1"
    android:layout_height="match_parent">
    <FrameLayout
        android:id="@+id/e"
        android:layout_width="match_parent"
        android:layout_height="300dp">

    </FrameLayout>
    <TextView
        android:id="@+id/btn"
        android:textColor="#ffffff"
        android:textSize="100dp"
        android:gravity="center"
        android:text="c"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:id="@+id/a"
    android:background="#4b14b1"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/btn"
        android:textColor="#ffffff"
        android:textSize="100dp"
        android:gravity="center"
        android:text="A"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:id="@+id/b"
    android:background="#4b14b1"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/btn"
        android:textColor="#ffffff"
        android:textSize="100dp"
        android:gravity="center"
        android:text="B"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>