addToBackStack方法不起作用

时间:2016-03-30 12:00:22

标签: android android-fragments

这是问题所在。我有大屏幕设备的布局

<LinearLayout
    android:id="@+id/container"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.lexzcq.diffsizes.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

            android:id="@+id/list_fragment"
            android:name="com.example.lexzcq.diffsizes.MainListFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="2"
            tools:layout="@android:layout/list_content"/>

        <fragment
            android:id="@+id/details_fragment"
            android:name="com.example.lexzcq.diffsizes.DetailsFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            tools:layout="@layout/details_fragment"/>
    </LinearLayout>
</LinearLayout>

这是我的活动的java代码

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements MainListFragment.onDetailFragmentGetText, DetailsFragment.OnSetDetailsText {
    Fragment listFragment;
    Fragment detailsFragment;
    FragmentTransaction transaction;
    TextView detailsText;
    String fromListText = "";

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);

        if (getDiagonal () >= 7 && getResources ().getConfiguration ().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            setContentView (R.layout.avtivity_main_pads);
            listFragment = new MainListFragment ();
            transaction = getSupportFragmentManager ().beginTransaction ();
            transaction.replace (R.id.list_fragment, listFragment)
                    .addToBackStack (null)
                    .commit ();
        } else {
            setContentView (R.layout.activity_main_vertical_phone);
            listFragment = new MainListFragment ();
            transaction = getSupportFragmentManager ().beginTransaction ();
            transaction.replace (R.id.container, listFragment)
            .addToBackStack (null)
            .commit ();
        }


    }

    public void onGetText (String s) {
        detailsFragment = new DetailsFragment ();
        transaction = getSupportFragmentManager ().beginTransaction ();
        transaction.replace (R.id.container, detailsFragment)
        .addToBackStack (null)
        .commit ();
        fromListText = s;

    }

    public void setDetailsText () {
        detailsText = (TextView) findViewById (R.id.details_container);
        detailsText.setText (fromListText);
    }

    private int getDiagonal () {
        DisplayMetrics metrics = new DisplayMetrics ();
        getWindowManager ().getDefaultDisplay ().getMetrics (metrics);

        int widthPixels = metrics.widthPixels;
        int heightPixels = metrics.heightPixels;

        float widthDpi = metrics.xdpi;
        float heightDpi = metrics.ydpi;

        float widthInches = widthPixels / widthDpi;
        float heightInches = heightPixels / heightDpi;

        double diagonalInches = Math.sqrt ((widthInches * widthInches) + (heightInches * heightInches));

        return (int) diagonalInches;

    }

}

每次我在列表片段中的文章之间切换细节片段更改和旧片段添加到backstack(在onGetText()方法中)但是当我按下按钮时没有任何反应。我试图用旧的和用popBackStack()方法替换新的片段来覆盖OnBackPressed方法,但它不起作用。 有人能告诉我如何使用backstack和两个(或更多)片段吗?

0 个答案:

没有答案