gpt4 book ai didi

java - Android addToBackStack(null) 不起作用,关闭应用程序

转载 作者:行者123 更新时间:2023-12-01 10:27:51 25 4
gpt4 key购买 nike

在我的应用程序中,我在一个 Activity 中使用 fragment ,并且我正在尝试替换它们。这工作正常,但在返回时我想切换到上一个 fragment 。我找到了 addToBackStack() 方法,但它不起作用。这是MainActivity中的代码:

MainActivity.java:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fragment1 fragment1 = new Fragment1();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(android.R.id.content, fragment1);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}

public void changeFragment(Fragment f)
{
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(android.R.id.content, f);
fragmentTransaction.commit();
}

@Override
public void onBackPressed()
{
if (getSupportFragmentManager().getBackStackEntryCount() > 0 )
{
getSupportFragmentManager().popBackStackImmediate();
}
else
{
super.onBackPressed();
}
}
}

Fragment1.java:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class Fragment1 extends Fragment
{
private Button buttonFragment1;

public Fragment1() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment1, container, false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
buttonFragment1 = (Button)getActivity().findViewById(R.id.buttonFragment1);
buttonFragment1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Fragment2 fragment2 = new Fragment2();
((MainActivity) getActivity()).changeFragment(fragment2);
}
});
}
}

Fragment2.java:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment2 extends Fragment {


public Fragment2()
{
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment2, container, false);
}

}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.asteam.myapplication.MainActivity">

<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fraggment" />
</RelativeLayout>

fragment_fragment1.xml:

<FrameLayout 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"
tools:context="com.example.asteam.myapplication.Fragment1">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonFragment1"
android:text="Send to fragment2"/>

</RelativeLayout>

</FrameLayout>

fragment_fragment2.xml:

<FrameLayout 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"
tools:context="com.example.asteam.myapplication.Fragment2">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment2" />

</RelativeLayout>

</FrameLayout>

和 list xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.asteam.myapplication">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

我尝试重写onBackPressed()方法:

@Override
public void onBackPressed()
{
if (getSupportFragmentManager().getBackStackEntryCount() > 0 )
{
getSupportFragmentManager().popBackStackImmediate();
}
else
{
super.onBackPressed();
}
}

但它不起作用,如果我在单击按钮后显示堆栈编号,我会得到1。我尝试将 android.support.v4. 更改为 android. 到 java fragment 文件和主 Activity,并将 MainActivity 更改为从 Activity 扩展,但这也不起作用。我究竟做错了什么?我的应用程序支持 API 14 及更高版本。任何帮助将不胜感激。

已编辑

加上 logcat:

02-08 14:38:49.191 10417-10417/? I/art: Not late-enabling -Xcheck:jni (already on)
02-08 14:38:49.266 10417-10417/com.example.asteam.myapplication W/System: ClassLoader referenced unknown path: /data/app/com.example.asteam.myapplication-2/lib/x86
02-08 14:38:49.322 10417-10432/com.example.asteam.myapplication D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
02-08 14:38:49.452 10417-10432/com.example.asteam.myapplication I/OpenGLRenderer: Initialized EGL, version 1.4
02-08 14:38:49.489 10417-10432/com.example.asteam.myapplication W/EGL_emulation: eglSurfaceAttrib not implemented
02-08 14:38:49.489 10417-10432/com.example.asteam.myapplication W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabebfe80, error=EGL_SUCCESS
02-08 14:38:55.915 10417-10432/com.example.asteam.myapplication E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab852fc0

最佳答案

像这样改变你的方法

public void changeFragment(Fragment f)
{
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(android.R.id.content, f);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}

然后您应该从 MainActivityonCreate 中删除 fragmentTransaction.addToBackStack(null);

此外,在 Fragment1 中,onActivityCreated 内的代码应移至 onCreateView

关于java - Android addToBackStack(null) 不起作用,关闭应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35270889/

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