对于我的应用程序,某些片段要求用户在显示之前在对话框中输入一个图钉。我有一个BaseFragment类,如果需要引脚,我所有其他片段都会扩展存储。我现在的问题是处理后退按钮,好像用户试图回到需要首先显示引脚对话框所需的引脚的片段。
目前我正在覆盖onBackPressed(),其中我想查看backstack以查看如果调用popBackstack()将会恢复哪些片段,这样我就可以检查是否应该显示pin对话框。下面是我当前的代码,popFragment将返回null:
@Override
public void onBackPressed() {
if(getSupportFragmentManager().findFragmentById(R.id.fragment_container) instanceof BaseFragment) {
BaseFragment frag = (BaseFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_container);
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
int index = getSupportFragmentManager().getBackStackEntryCount() - 1;
Log.e("Back stack", Integer.toString(index));
FragmentManager.BackStackEntry backEntry = (FragmentManager.BackStackEntry) getSupportFragmentManager().getBackStackEntryAt(index);
BaseFragment popFragment = (BaseFragment) getSupportFragmentManager().findFragmentById(backEntry.getId());
BaseFragment.checkAuth(this, frag, popFragment, new NavigationCallback((AppCompatActivity) this));
} else {
super.onBackPressed();
}
} else {
super.onBackPressed();
}
}
我已经检查过backEntry是否为null而不是。 backEntry.getId()也返回值为1,因此我不确定为什么findFragmentById()为null。我也不认为它与我如何显示片段有关,但这里也是我的代码:
FragmentManager fragManager = mActivity.getSupportFragmentManager();
FragmentTransaction transaction = fragManager.beginTransaction();
transaction.replace(R.id.fragment_container, mFragment);
transaction.addToBackStack(null);
transaction.commit();
答案 0 :(得分:2)
BackStackEntry
ID和Fragment
ID与彼此无关。
我通过使用标签做了类似于你正在做的事情。同样,BackStackEntry
代码和Fragment
代码彼此无关;但是,通过对两个标签使用相同的值,您可以实现目标。
String tag = ...
FragmentManager fragManager = mActivity.getSupportFragmentManager();
FragmentTransaction transaction = fragManager.beginTransaction();
// tag is associated with fragment
transaction.replace(R.id.fragment_container, mFragment, tag);
// same tag is associated with back stack entry
transaction.addToBackStack(tag);
transaction.commit();
...
int count = fragmentManager.getBackStackEntryCount();
if (count > 0) {
FragmentManager.BackStackEntry topBackStackEntry =
fragmentManager.getBackStackEntryAt(count - 1);
String tag = topBackStackEntry.getName();
if (tag != null) {
Fragment fragment = fragmentManager.findFragmentByTag(tag);
if (fragment != null) {
// we found the fragment
// you can also sanity-check here with fragment.isResumed()
}
}
}
答案 1 :(得分:2)
您不见了Frag-A-> Frag-B-> Frag-C。现在,您想返回Frag-A而不为Frag-A创建新的Fragment视图吗? 解决方法如下:
def split_into_batches(all_users, batch_size=BATCH_SIZE):
"""h/t: https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks"""
for i in range(0, len(all_users), batch_size):
yield all_users[i : i + batch_size]
def process_and_save_batch(user_rows, bq):
print(generate_timestamp(), "|", current_thread().name, "|", "PROCESSING...")
bq.append_user_friends([user_with_friends(user_row) for user_row in user_rows])
print(generate_timestamp(), "|", current_thread().name, "|", "PROCESSED BATCH OF", len(user_rows))
return True
service = BigQueryService() # a custom class defined elsewhere
users = service.fetch_remaining_users(min_id=MIN_ID, max_id=MAX_ID, limit=LIMIT)
print("FETCHED UNIVERSE OF", len(users), "USERS")
batches = list(split_into_batches(users))
print(f"ASSEMBLED {len(batches)} BATCHES OF {BATCH_SIZE}")
with ThreadPoolExecutor(max_workers=MAX_THREADS, thread_name_prefix="THREAD") as executor:
for batch in batches:
executor.submit(process_and_save_batch, batch, service)
还删除您的导航条目action_Frag_C_to_Frag_A。如果您不小心创建了导航图。