Start activity without showing it

前端 未结 5 539
失恋的感觉
失恋的感觉 2020-12-15 22:16

I have an activity A, which starts activity B and activity B starts activity C. Is there a way not to show activity B. I want this behavour because if there is no content (e

相关标签:
5条回答
  • 2020-12-15 22:25

    I found the solution:

    • Activity A starts activity B with extra parameter (intent.putExtra("something", true))
    • Activity B:
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getIntent().hasExtra("something") && getIntent().getBooleanExtra("something", false) {
                //show activity B
            setContentView(R.layout.activity_B);
        } else {
                //don't show activity B, start activity C
            startActivityForResult(activity_C, ACTIVITY_NOT_INITIALIZED); //start activity C
        }
    }
    

    //when came back from activity C, check if activity B was already initialized

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == ACTIVITY_NOT_INITIALIZED) {
            //show activity B
            setContentView(R.layout.activity_B);
      }
    }
    
    0 讨论(0)
  • 2020-12-15 22:35

    You can call startActivity, without calling setContentView for a layout. Hope this helps!

    0 讨论(0)
  • 2020-12-15 22:36

    In your manifest set

    android:theme="@android:style/Theme.NoDisplay"
    

    for your activity.

    0 讨论(0)
  • 2020-12-15 22:36

    do not set setContentView() for activity B

    0 讨论(0)
  • 2020-12-15 22:44

    just call startActivity() in onCreate() method of activity B. It will not be shown, but it will be in activity stack.

    0 讨论(0)
提交回复
热议问题