使用堆栈导航回到片段

时间:2018-12-15 21:50:06

标签: android

所以我有4个底部导航部分,我在使用片段,但是我有一个问题,一旦我插入数据,数据就没有填充,一旦添加了所有需要的事务导航,我该如何回到我正确的片段说回到storeExpense上的TransactionFragment。

这是我的代码:

private void storeExpense(ExpenseData expenseData) {

    ContentValues values = new ContentValues();
    values.put(ExpensesContract.ExpenseEntry.COLUMN_EXPENSE_NAME, expenseData.getExpenseName());
    values.put(ExpensesContract.ExpenseEntry.COLUMN_EXPENSE_TYPE, expenseData.getExpenseType());
    values.put(ExpensesContract.ExpenseEntry.COLUMN_EXPENSE_CURRENCY,expenseData.getCurrency());
           values.put(ExpensesContract.ExpenseEntry.COLUMN_EXPENSE_CREATED_DATE, expenseData.getExpenseDate());
    values.put(ExpensesContract.ExpenseEntry.COLUMN_EXPENSE_USER_ID, new ConstantSess(getActivity().getBaseContext()).getuserId());
    Uri uri = getActivity().getContentResolver().insert(ExpensesContract.ExpenseEntry.CONTENT_URI, values);

    if (uri == null) {
        Toast.makeText(this.getActivity(), R.string.editor_save_expense_failed,
                Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this.getActivity(), R.string.editor_save_expense_successful,
                Toast.LENGTH_SHORT).show();
        checkBudgets(expenseData.getExpenseName());
    }

    getActivity().finish();

I want to add something like this      Intent intent = new Intent(this.getActivity(), TransactionsFragment.class);             startActivity(intent);    }

getActitivy().finish()之后,我想回到我的TransactionFragment中,如何安全地做到这一点?

此代码使我的应用程序崩溃

 Fragment someFragment = new TransactionsFragment();
 FragmentTransaction transaction = getFragmentManager().beginTransaction();
 transaction.replace(R.id.container, someFragment, "Transaction");
 transaction.addToBackStack("Transaction");
 transaction.commit();

错误:

 java.lang.IllegalArgumentException:

1 个答案:

答案 0 :(得分:0)

它的工作原理如下:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.container, new TransactionsFragment()).commit();

返回:

if (getSupportFragmentManager().getBackStackEntryCount() != 0) {
    getSupportFragmentManager().popBackStack();
}