我目前正在开发一个Android应用程序,它使用带有Fragments的视图寻呼机页面来布局选项卡中的主要活动。大部分时间这对我们来说都很好,但我们目前遇到的问题是,在某些情况下,当应用程序恢复/创建时,Fragment视图为空。这似乎只有在应用程序通过活动意图调用或通过在应用程序在后台时单击通知来恢复时才会发生。仅当应用程序已在后台加载时才会出现此问题,如果操作启动活动并创建所有新内容,则问题不会发生。
以下是设置我们的视图寻呼机的代码。
主要活动
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager().enableDebugLogging(true);
viewPager = (ViewPager) findViewById(R.id.mainViewPager);
viewPager.setAdapter(new MainPagerAdapter(getSupportFragmentManager(), this));
// Bind the tabs to the ViewPager
PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
tabs.setViewPager(viewPager);
tabs.setUnderlineHeight(4);
}
寻呼机适配器
public class MainPagerAdapter extends FragmentStatePagerAdapter {
Context context;
public MainPagerAdapter(FragmentManager fragmentManager, Context context) {
super(fragmentManager);
this.context = context;
}
// Returns the fragment to display for that page
@Override
public Fragment getItem(int position) {
return MainPagerEnum.values()[position].getFragment();
}
@Override
public int getCount() {
return MainPagerEnum.values().length;
}
@Override
public CharSequence getPageTitle(int position) {
MainPagerEnum mainPagerEnum = MainPagerEnum.values()[position];
return context.getString(mainPagerEnum.getTitleResId());
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
Page Enum
public enum MainPagerEnum {
YOURACCOUNT(R.string.your_account, AccountFragment.newInstance()),
LOCATIONS(R.string.locations, LocationsFragment.newInstance()),
OCCURANCES(R.string.occurances, OccurancesFragment.newInstance());
private int titleResId;
private Fragment fragment;
MainPagerEnum(int titleResId, Fragment fragment) {
this.titleResId = titleResId;
this.fragment = fragment;
}
public int getTitleResId() {
return this.titleResId;
}
public Fragment getFragment() {
return this.fragment;
}
其中一个以零视图结尾的片段
public class AccountFragment extends Fragment {
private static final String TAG = "AccountFragment";
public AccountFragment() {
}
public static AccountFragment newInstance() {
AccountFragment fragment = new AccountFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_account, container, false);
LinearLayout signInLayout = (LinearLayout) view.findViewById(R.id.sign_in_block);
LinearLayout signedInLayout = (LinearLayout) view.findViewById(R.id.signed_in_block);
TextView welcomeFamily = (TextView) view.findViewById(R.id.welcome_family);
ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.account_fetch_progress);
Button login = (Button) view.findViewById(R.id.loginButton);
if(AccountProperties.getActiveAccount() != null) {
AccountFragmentUtil.showHideAccountElements(signInLayout, signedInLayout, welcomeFamily, progressBar, login, getContext());
}
return view;
}
@Override
public void onDetach() {
super.onDetach();
}
在片段管理器上使用调试日志记录我已经确定问题与backstack有关。如果将应用程序置于后台并且用户通过从之前的应用程序切换器单击该应用程序来恢复该应用程序,则没有问题,并且日志如下所示。
08-29 09:03:42.566 29172-29205 / com.app D / OpenGLRenderer:交换行为1 08-29 09:03:45.805 29172-29172 / com.app V / FragmentManager:movefrom RESUMED:LocationsFragment {5455ec1#0 id = 0x7f0d00a1} 08-29 09:03:45.805 29172-29172 / com.app V / FragmentManager:movefrom RESUMED:AccountFragment {9913fcb#1 id = 0x7f0d00a1} 08-29 09:03:46.068 29172-29172 / com.app V / FragmentManager:LocationsFragment的保存状态{5455ec1#0 id = 0x7f0d00a1}:Bundle [{android:user_visible_hint = false,android:view_state = {2131558528 = android。 view.AbsSavedState$1@af5a138,2131558554=android.view.AbsSavedState$1@af5a138,2131558605=android.view.AbsSavedState$1@af5a138,2131558606=android.view.AbsSavedState$1@af5a138,2131558607=android.view.AbsSavedState$1@af5a138 ,2131558608 = android.view.AbsSavedState$1@af5a138}}] 08-29 09:03:46.068 29172-29172 / com.app V / FragmentManager:AccountFragment的保存状态{9913fcb#1 id = 0x7f0d00a1}:Bundle [{android:view_state = {2131558571 = android.widget.ProgressBar $ SavedState @ b04e511,2131558572 = HorizontalScrollView.SavedState {b06e676 scrollPosition = 0},2131558573 = android.view.AbsSavedState$1@af5a138,2131558574 = TextView.SavedState {16c4477 start = 0 end = 0},2131558575 = android.view.AbsSavedState$1@af5a138 ,2131558577 = android.view.AbsSavedState$1@af5a138,2131558578 = TextView.SavedState {2ff98e4 start = 0 end = 0},2131558579 = android.view.AbsSavedState$1@af5a138,2131558580=android.view.AbsSavedState$1@af5a138,2131558581 =android.view.AbsSavedState$1@af5a138,2131558582=android.view.AbsSavedState$1@af5a138,2131558583=android.view.AbsSavedState$1@af5a138,2131558584=android.view.AbsSavedState$1@af5a138,2131558585 = android.view.AbsSavedState $ 1 @ af5a138,2131558586=android.view.AbsSavedState$1@af5a138,2131558587=android.view.AbsSavedState$1@af5a138,2131558588 = android.view.AbsSav edState $ 1 @ af5a138,2131558589=android.view.AbsSavedState$1@af5a138,2131558590=android.view.AbsSavedState$1@af5a138,2131558591=android.view.AbsSavedState$1@af5a138,2131558592=android.view.AbsSavedState$1@af5a138}} ] 08-29 09:03:46.068 29172-29172 / com.app V / FragmentManager:saveAllState:添加片段#0:AccountFragment {9913fcb#1 id = 0x7f0d00a1} 08-29 09:03:46.068 29172-29172 / com.app V / FragmentManager:saveAllState:添加片段#1:LocationsFragment {5455ec1#0 id = 0x7f0d00a1} 08-29 09:03:46.077 29172-29172 / com.app V / FragmentManager:movefrom STARTED:LocationsFragment {5455ec1#0 id = 0x7f0d00a1} 08-29 09:03:46.077 29172-29172 / com.app V / FragmentManager:movefrom STARTED:AccountFragment {9913fcb#1 id = 0x7f0d00a1} 08-29 09:03:46.082 29172-29172 / com.app V / FragmentManager:movefrom STOPPED:LocationsFragment {5455ec1#0 id = 0x7f0d00a1} 08-29 09:03:46.082 29172-29172 / com.app V / FragmentManager:movefrom STOPPED:AccountFragment {9913fcb#1 id = 0x7f0d00a1}
但是,如果应用程序已经在后台并且类似于通知或帐户管理器尝试像下面那样启动它,则会在日志中看到有关backstack的以下注释,并且片段视图为空。
PackageManager manager = mContext.getPackageManager();
Intent tempIntent = manager.getLaunchIntentForPackage("com.app");
tempIntent.addCategory(Intent.CATEGORY_LAUNCHER);
intent = new Intent(tempIntent);
08-29 09:09:47.630 29172-29172 / com.app V / FragmentManager:moveto STARTED:AccountFragment {9913fcb#1 id = 0x7f0d00a1} 08-29 09:09:47.630 29172-29172 / com.app V / FragmentManager:moveto RESUMED:AccountFragment {9913fcb#1 id = 0x7f0d00a1} 08-29 09:09:47.630 29172-29172 / com.app V / FragmentManager:Commit:BackStackEntry {895f2b9} 08-29 09:09:47.630 29172-29172 / com.app D / FragmentManager:mName = null mIndex = -1 mCommitted = false 08-29 09:09:47.630 29172-29172 / com.app D / FragmentManager:操作: 08-29 09:09:47.630 29172-29172 / com.app D / FragmentManager:Op#0:添加AccountFragment {9913fcb#1 id = 0x7f0d00a1} 08-29 09:09:47.630 29172-29172 / com.app D / FragmentManager:Op#1:ADD LocationsFragment {5455ec1#0 id = 0x7f0d00a1} 08-29 09:09:47.630 29172-29172 / com.app V / FragmentManager:运行:BackStackEntry {895f2b9} 08-29 09:09:47.630 29172-29172 / com.app V / FragmentManager:add:AccountFragment {9913fcb#1 id = 0x7f0d00a1} 08-29 09:09:47.630 29172-29172 / com.app V / FragmentManager:add:LocationsFragment {5455ec1#0 id = 0x7f0d00a1}