无法从主活动的onCreate()启动新活动

时间:2018-07-31 16:26:40

标签: android android-intent android-activity

如果要立即从主活动中首次启动该应用程序,但我想开始一个新活动DirectorySelectionActivity,但是我的主活动的onCreate()功能没有按照我的预期启动新活动。相反,我的主要活动完成了onCreate()函数,并且只有在完成oncreate()函数后,它才继续进入我的DirectorySelectionActivity。在完成onCreate()之前,如何在主要活动的onCreate()函数中间移动到新活动?这就是我所拥有的:

MainActivity.java

public class MainActivity extends AppCompatActivity {        
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);


        // If this is the first time user has run this app
         if(true){ // for testing
            Intent intent = new Intent(this, DirectorySelectionActivity.class);
            /* I've also tried:
               Intent intent = new Intent(MainActivity.this, DirectorySelectionActivity.class);
            */
            startActivity(intent);
            // Also tried:
            //this.startActivity(intent);

        }


        // This happens before my DirectorySelectionActivity activity gets started

        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = fm.findFragmentById(R.id.fragment_container);
        if (fragment == null) {
            fragment = createFragment();
            fm.beginTransaction().add(R.id.fragment_container, fragment).commit();
        }
    }
}

DirectorySelectionActivity.java

public class DirectorySelectionActivity extends AppCompatActivity  {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);



        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = fm.findFragmentById(R.id.fragment_container);
        if (fragment == null) {
            fragment = SelectUnwantedDirsFragment.newInstance();
            fm.beginTransaction().add(R.id.fragment_container, fragment).commit();
        }

    }

}

5 个答案:

答案 0 :(得分:0)

用else语句包装

 // If this is the first time user has run this app
         if(true){ // for testing
            Intent intent = new Intent(this, DirectorySelectionActivity.class);
            /* I've also tried:
               Intent intent = new Intent(MainActivity.this, DirectorySelectionActivity.class);
            */
            startActivity(intent);
            // Also tried:
            //this.startActivity(intent);

        }else{



        // This happens before my DirectorySelectionActivity activity gets started

        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = fm.findFragmentById(R.id.fragment_container);
        if (fragment == null) {
            fragment = createFragment();
            fm.beginTransaction().add(R.id.fragment_container, fragment).commit();
        }
    }
}

如果您不使用else,它将执行您的第一个代码,然后继续执行,直到完成所有onCreate()内部的执行

它也会首先膨胀您的片段,因为您的

 setContentView(R.layout.activity_fragment); 

您可以尝试在setContentView()之前执行代码

答案 1 :(得分:0)

尝试一下:

  if(true){
    Intent intent = new Intent(this, DirectorySelectionActivity.class);
    startActivity(intent);
    return;
  }

答案 2 :(得分:0)

如果您从setContentView(R.layout.activity_fragment);的{​​{1}}中删除了MainActivity,那么它将不会显示。
但是,如果您希望在某个时候显示它怎么办?
我建议使用

这样的布尔值标志
onCreate()

答案 3 :(得分:0)

您如何引入一个扩展Application类的新类?因此,如果可以在DirectorySelectionActivity之前开始setContentView(R.layout.activity_fragment); 我将在该类下面放置代码。

        Intent intent = new Intent(this, DirectorySelectionActivity.class);
        /* I've also tried:
           Intent intent = new Intent(MainActivity.this, DirectorySelectionActivity.class);
        */
        startActivity(intent);
        // Also tried:
        //this.startActivity(intent);

根据文档,在创建应用程序/包的过程时,将在任何其他类之前实例化Application类或Application类的子类。另外,不要忘记更新清单文件。 请看看https://developer.android.com/reference/android/app/Application

答案 4 :(得分:-1)

在startActivity(intent)之后立即调用finish():

    Intent intent = new Intent(this, DirectorySelectionActivity.class);
    startActivity(intent);
    finish();