片段上的屏幕旋转数据消失了吗?

时间:2018-09-16 14:01:06

标签: android android-fragments

如何保留其中一个片段上的数据?所以我有一个底部导航器,到目前为止我有四个项目可以保留,然后onRotate,但是我有这个片段(BudgetFragment),它不保留数据,但是一旦我移到下一个底部导航项目并返回就可以恢复。我尝试捆绑对象,但似乎不起作用,这是我尝试过的方法。

注意:我尝试在BudgetFragment类上使用setRetainInstance(true);,但是它仍然不显示输入的预算信息。因此,一旦onRotate我该如何保留这些数据。

我的主要方法。

public class MainActivity extends AppCompatActivity {
    private Fragment fragment;
    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        fragment = getSupportFragmentManager().findFragmentByTag(TAG);
        if (fragment == null) {
            fragment = new TransactionsFragment();
        }

        BottomNavigationView navigation = findViewById(R.id.navigation);
        BottomNavigationViewHelper.disableShiftMode(navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.content, fragment);
        transaction.commit();


    }



   private final BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
           = item -> {
               List<Fragment> fragments = getSupportFragmentManager().getFragments();
               boolean isOnStack = false;
               switch (item.getItemId()) {
                   case R.id.action_transactions:
                       fragment = new TransactionsFragment();
                       for(Fragment frag:fragments){
                           /* Check if fragment is on stack*/
                           if(frag instanceof TransactionsFragment){
                               isOnStack = true;
                               break;
                           }
                       }
                       break;
                   case R.id.action_budget:
                       fragment = new BudgetFragment();
                       for(Fragment frag:fragments){
                           /* Check if fragment is on stack*/
                           if(frag instanceof BudgetFragment){
                               isOnStack = true;
                               break;
                           }
                       }


                   case R.id.action_task:
                       fragment = new TaskFragment();
                       for(Fragment frag:fragments){
                           /* Check if fragment is on stack*/
                           if(frag instanceof TaskFragment){
                               isOnStack = true;
                               break;
                           }
                       }
                       break;
               }
               /*
                   Prevent duplicate record on back stack and keep consistent back navigation
                */
               if (!isOnStack)
                   getSupportFragmentManager().beginTransaction()
                           .replace(R.id.content, fragment, TAG)
                           .addToBackStack(null)
                           .commit();

               return true;
           };


}

我的BudgetFragment不会在旋转时保留数据的:

public class BudgetFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
    private ArrayList<BudgetData> budgetDataArrayList = null;
    private RecyclerView recyclerView;

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_budget, container, false);


        recyclerView =  view.findViewById(R.id.budgets);
        recyclerView.addOnItemTouchListener(
                new RecyclerItemClickListener(this.getContext(), (view1, position) -> {
                    if (position == budgetDataArrayList.size() - 1) {
                        openExpenseActivity();
                    }
                })
        );

        getLoaderManager().initLoader(0, null, this);



        return view;
    }

    private void openExpenseActivity() {
        Intent intent = new Intent(this.getActivity(), AddBudgetActivity.class);
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }


    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        String[] projection = {
                BudgetsContract.BudgetsEntry._Id,
                BudgetsContract.BudgetsEntry.COLUMN_BUDGET_NAME,
                BudgetsContract.BudgetsEntry.COLUMN_BUDGET_AMOUNT,
                BudgetsContract.BudgetsEntry.COLUMN_BUDGET_EXPENSES,
                BudgetsContract.BudgetsEntry.COLUMN_BUDGET_START_DATE,
                BudgetsContract.BudgetsEntry.COLUMN_BUDGET_END_DATE,
                BudgetsContract.BudgetsEntry.COLUMN_BUDGET_NOTIFICATIONS};

        String selection = BudgetsContract.BudgetsEntry.COLUMN_BUDGET_USER_ID + "=?";

        String[] selectionArgs = {
                String.valueOf(new Session(getActivity().getBaseContext()).getuserId())
        };

        return new CursorLoader(this.getActivity(),
                BudgetsContract.BudgetsEntry.CONTENT_URI,
                projection,
                selection,
                selectionArgs,
                null);
    }

0 个答案:

没有答案