我正在尝试从 backstack 中检索片段。它正在被检索,但问题是在按下后退按钮时,oncreate 视图和当前片段的后续生命周期方法也被调用。这是我将片段放入 backstack 的代码:
fragment=new FileTrackingFragment();
bundle=new Bundle();
bundle.putString("name","Dak Monitoring");
bundle.putInt("num",2);
fragment.setArguments(bundle);
ft.replace(R.id.parent, fragment, fragment.getClass().getName());
ft.addToBackStack(fragment.getClass().getName());
ft.commit();
break;
Run Code Online (Sandbox Code Playgroud)
这是片段的代码:
package com.example.rajvirsingh.epunjaboffice.DakMonitoring;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.percent.PercentRelativeLayout;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.example.rajvirsingh.epunjaboffice.FileTracking.HistoryAdapter;
import com.example.rajvirsingh.epunjaboffice.MainActivity;
import com.example.rajvirsingh.epunjaboffice.R;
import com.example.rajvirsingh.epunjaboffice.Utility.Constants;
import com.example.rajvirsingh.epunjaboffice.Utility.SessionManager;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import org.json.JSONArray;
import java.util.ArrayList;
import …Run Code Online (Sandbox Code Playgroud) android android-fragments android-fragmentactivity fragment-lifecycle fragment-backstack
正如我所观察到的那样,进程死亡后似乎不会保留片段后退堆栈。因此,如果我通过将应用程序置于后台并调用它来杀死我的应用程序,am kill <app>那么如果我再次将应用程序置于前台,则不会重新创建片段后台堆栈。
这似乎是两种情况 - usingJetpack Navigation Components和 using supportfragmantmanagerwithaddToBackStack
方向更改后,一切都按预期进行。所以我不知道后台堆栈是否总是在进程死亡后被清除并且无法保留,或者是否必须执行一些额外的步骤来保留后台堆栈。
上面的标题已经被问了很多次,但答案似乎FragmentStatePagerAdapter与我的问题无关。我putFragment(Bundle, String, Fragment)直接用这个方法。
Android文档说putFragment(Bundle, String, Fragment):
Put a reference to a fragment in a Bundle. This Bundle can be persisted as saved state, and when later restoring getFragment(Bundle, String) will return the current instance of the same fragment.
Parameters
* bundle The bundle in which to put the fragment reference.
* key The name of the entry in the bundle.
* fragment The Fragment whose reference is to be stored.
Run Code Online (Sandbox Code Playgroud)
但是下面的代码会抛出异常:
Bundle …Run Code Online (Sandbox Code Playgroud) 我的问题一般在这个问题中描述: 一劳永逸,如何在后台堆栈中正确保存Fragments的实例状态?
但我无法理解接受的答案是如何与它相关的.
保持简短:
有2个片段A和B.
我使用transaction.replace()从A转到B并将事务添加到backstack.
我旋转屏幕 - >片段A不可见,但它的onSaveInstanceState被调用 - 到目前为止一切正常.
我再次旋转屏幕.再次调用A的onSaveInstanceState,但现在它的视图不存在,所以我无法保存它的状态.
如接受的答案所示,使用getFragment/putFragment的目的是什么?当我的片段在后台时,我猜是没有必要继续参考它.谢谢.
android android-fragments fragmentmanager fragment-backstack
我有两个片段A和B.
我已使用以下代码从A切换到B:
FragmentManager fragmentManager;
FragmentTransaction transaction;
fragmentManager = getActivity().getFragmentManager();
transaction = fragmentManager.beginTransaction();
transaction.setCustomAnimations(R.animator.enter_anim, R.animator.exit_anim,R.animator.popenter, R.animator.popexit);
transaction.replace(R.id.fragmentLayout, new B())
.addToBackStack("tag").commit();
Run Code Online (Sandbox Code Playgroud)
B包含SQLite操作(SugarORM).
从A切换到B时,即使我使用AsyncTask&IntentService进行数据库操作,屏幕也会冻结.
此外,当我按后退按钮[ getFragmentManager().popBackStack();]键,屏幕冻结了几秒钟,它切换从B.回A之前
的popBackStack动画太不可见.
我尝试在imageview中显示不确定的进度条和动画列表,但它们也冻结了,看起来好像它们是静态图像.
我在 中添加片段(主片段)Activity.onCreate(),而不将其添加到后台堆栈,我使用FragmentTransaction.replace(). 之后我使用添加每个下一个片段FragmentTransaction.replace()并将其添加到后台堆栈。
我有一个功能可以清除后退堆栈,使主页片段可见,为此我使用FragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);.
如果我只向后台堆栈添加 1 个片段,然后清除后台堆栈,则效果很好,但如果我添加多个片段,则在清除后台堆栈后,屏幕为空白,尽管主片段接收到 onStart() 和 onResume() 并认为它是可见的,它不会收到 onPause()、onStop(),直到我再次添加另一个片段或离开应用程序。另外,如果我popBackstack()一一地(在用户操作上)它工作正常,但如果我popBackstack()在循环中(一次弹出所有片段)它就不起作用。
这是代码:
public void changeFragment(BaseFragment fragment, boolean addToBackStack, boolean preventDuplicate) {
Fragment topFragment = getSupportFragmentManager().findFragmentById(fragmentContainer.getId());
if (preventDuplicate && topFragment != null && fragment.getClass().equals(topFragment.getClass())) {
//Prevent adding same fragment
return;
}
FragmentTransaction transaction =
fragmentManager
.beginTransaction()
.replace(fragmentContainer.getId(), fragment);
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.commit();
}
public void goToHome() {
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
Run Code Online (Sandbox Code Playgroud) android android-fragments fragmentmanager fragment-backstack
我想要的正确行为是:
Fragment A -> Fragment B -> Fragment C -> Fragment A
Run Code Online (Sandbox Code Playgroud)
我目前所做的:
popBackStack()still 这里一切顺利。popBackStack()但 BackStackEntryCount 仍将包含 B 的条目。我真的需要backStackEntryCount与管理器中包含的片段相同。
有人知道我做错了什么吗?
我的代码:
Fragment fragment1 = fragmentManager.findFragmentByTag("NavigationFragment_");
if (fragment1 != null) {
fragmentManager.beginTransaction()
.setTransition(TRANSIT_FRAGMENT_FADE)
.remove(fragment1)
.commit();
}
fragmentManager.beginTransaction()
.addToBackStack(backstack)
.setTransition(TRANSIT_FRAGMENT_OPEN)
//.remove(getFragmentManager().findFragmentByTag(NavigationFragment_.class.getSimpleName()))
.add(R.id.fragmentContainer, fragment, fragment.getClass().getSimpleName())
.commit();
Run Code Online (Sandbox Code Playgroud)
我一直在寻找解决方案,但没有结果,所以请不要将此标记为重复。
提前致谢。
java android android-fragments fragmentmanager fragment-backstack
我是Android Jetpack导航架构的新手。我正在一个新的应用程序上尝试。有一个活动和一些片段,其中两个是登录屏幕和电子邮件登录屏幕。我在导航XML中定义了这些片段。该应用程序的流程如下:
Login screen ? Email Login screen
我想要的是,导航到电子邮件登录屏幕后,当我按返回时,该应用程序退出。表示已删除登录屏幕的后堆栈。我知道登录屏幕不应该这样工作,但我仍然只是想办法解决。
我遵循了Google 导航组件入门中的文档。它说,使用app:popUpTo和apppopUpToInclusive="true"应该清除后台堆栈,但是当我在电子邮件登录屏幕上按回去时,它仍然返回登录而不是退出。
所以,这就是我尝试过的。
nav_main.xml
<fragment android:id="@+id/loginFragment"
android:name="com.example.myapp.ui.main.LoginFragment"
android:label="@string/login"
tools:layout="@layout/fragment_login" >
<action
android:id="@+id/action_login_to_emailLoginFragment"
app:destination="@id/emailLoginFragment"
app:popEnterAnim="@anim/slide_in_right"
app:popExitAnim="@anim/slide_out_right"
app:popUpTo="@+id/emailLoginFragment"
app:popUpToInclusive="true"/>
</fragment>
<fragment android:id="@+id/emailLoginFragment"
android:name="com.example.myapp.ui.main.EmailLoginFragment"
android:label="EmailLoginFragment"
tools:layout="@layout/fragment_login_email" />
Run Code Online (Sandbox Code Playgroud)
LoginFragment.kt
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding.emailLoginButton.setOnClickListener {
findNavController().navigate(R.id.action_login_to_emailLoginFragment)
}
return binding.root
}
Run Code Online (Sandbox Code Playgroud)
我给按钮单击事件。在其中,我使用了导航控制器,通过为其提供操作的ID导航到电子邮件登录屏幕。在中<action>,有app:popUpTo和app:popUpToInclusive="true"。
一遍又一遍地阅读文档,并阅读了许多StackOverflow问题之后,我发现这些属性应该可以将我的登录屏幕从后栈中删除。但是他们没有。该按钮确实导航到电子邮件登录屏幕,但是当我按返回时,它仍然返回登录屏幕,而不是退出应用程序。我想念什么?
navigation android kotlin fragment-backstack android-jetpack
我创建了一个导航图,其中包含片段fragA->fragB->fragC-fragD->fragE->fragF->fragG。从某些推送通知中,用户必须直接使用 转到fragG findNavController().navigate(R.id.fragG),当用户点击后退按钮时,他必须转到fragF,但现在回到导航图的第一个片段,因为没有添加fragB->fragC-fragD->fragE->fragF到返回堆栈。当用户导航到最后一个按后退按钮时,可以将此片段添加到堆栈中吗?谢谢。
android android-fragments kotlin fragment-backstack android-architecture-navigation
我有一个活动应用程序,包含三个片段,A、B、C。在正常的应用程序流程中,片段按顺序打开:A->B->CI 有一个前台服务,并带有通知,单击时应打开片段C. 我使用导航架构组件,并将目的地作为深层链接添加到通知中:
NotificationCompat.Builder(context, NOTIFICATION_CHANNEL)
....
.setContentIntent(NavDeepLinkBuilder(context)
.setGraph(R.navigation.main_navigation)
.setDestination(R.id.fragmentC)
.setArguments(bundle)
.createPendingIntent())
.build()
Run Code Online (Sandbox Code Playgroud)
未构建片段后台堆栈。当我从片段 C 导航回来时,我立即到达片段 A,而不是 B。根据导航原则, backstack 应该是自然的,但我似乎无法实现。我在这里缺少什么?谢谢你。
android android-fragments fragment-backstack deeplink navigation-architecture
android ×10
kotlin ×2
android-architecture-navigation ×1
deeplink ×1
fragment ×1
freeze ×1
java ×1
navigation ×1