简体   繁体   中英

Android Activity unexpected Activity open instead of change Fragment

Hello when I directly open Stages ( Activity ) with those Fragments inside it then everythings swiches correclty. I guess the problem starts when I open another Activities before switching Fragments in Stages .

I can add that every Activity has such code:

@Override
public void onBackPressed() {
    Intent intent = new Intent(Info.this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    startActivity(intent);
    finish();
    super.onBackPressed();
}

and swiching fragments looks like this:

public void showFragment(int viewId) {
    getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
    Fragment fragment = null;

    switch (viewId) {
        case R.id.navigation_stage_zero:
            if (bottomNaviView.getSelectedItemId() != R.id.navigation_stage_zero) {
                fragment = stageZeroFragment;
                newPosition = 0;
            }
            break;
        case R.id.navigation_stage_one:
            if (bottomNaviView.getSelectedItemId() != R.id.navigation_stage_one) {
                fragment = stageOneFragment;
                newPosition = 1;
            }
            break;
        case R.id.navigation_stage_two:
            if (bottomNaviView.getSelectedItemId() != R.id.navigation_stage_two) {
                fragment = stageTwoFragment;
                newPosition = 2;
            }
            break;
        case R.id.navigation_stage_three:
            if (bottomNaviView.getSelectedItemId() != R.id.navigation_stage_three) {
                fragment = stageThreeFragment;
                newPosition = 3;
            }
            break;
    }

    if (fragment != null) {
        if (startingPosition > newPosition) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right)
                    .replace(R.id.content_frame, fragment).commit();
        }
        if (startingPosition < newPosition) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left)
                    .replace(R.id.content_frame, fragment).commit();
        }

        startingPosition = newPosition;
    }
}

So why does sometimes Activity (history or such kind of cache?) open when I switch fragments?

if you previous activity is mainActivty, doesn't need to start another intent in onBackPressed, to return to mainActivity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM