Not start MainActivity with android 2.3

2019-03-04 04:47发布

问题:

I want to start my MainActivity, in android 2.3, it will not start, but in Jelly Bean it works. I get no error, nothing happens!

I've noticed when I use putExtra, then it does not work. Is there a reason?

start MainActivity

final ComponentName cn = new ComponentName("de.application.ui", "de.application.ui.MainActivity");

    Intent intent = IntentCompat.makeRestartActivityTask(cn);
    intent.putExtra("de.application.exit", true); // I have this line in it yet
    activity.startActivity(intent);

Manifest

<activity
        android:theme="@style/Theme"
        android:name=".ui.MainActivity"
        android:launchMode="singleTop">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

My solution that works.

start MainActivity

   Intent intent = new Intent(activity, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("de.application.exit", true);
    activity.finish();
    activity.startActivity(intent);

Manifest

<activity
        android:theme="@style/Theme"
        android:name=".ui.MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

回答1:

I do this to start a main activity:

Intent intent = IntentCompat.makeRestartActivityTask(cn);
activity.startActivity(intent);


        Intent i =  new Intent("android.intent.action.MAIN");
        final ComponentName cn = new ComponentName  ("de.application.ui", "de.application.ui.MainActivity");

                        i.setComponent(n);                          

                    try {
                        startActivity(i);
                    } 
                    catch (ActivityNotFoundException e) {

                    }


回答2:

Might want to give this a try:

    Intent intent = new Intent("android.intent.action.MAIN");
    intent.addCategory("android.intent.category.LAUNCHER");
    intent.setComponent(new ComponentName("de.application.ui", "de.application.ui.MainActivity"));
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra("de.application.exit", true);
    startActivity(intent);