简体   繁体   中英

Android - maps doesn't work if they are not main activity

I created application with maps. It works fine but I've got one problem. Right now activity with maps is main activity. Manifest looks like this :

<meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />
        <activity
            android:name=".View.MainActivity"
            android:label="@string/title_activity_main" >

        </activity>
        <activity
            android:name=".View.MapsActivity"
            android:label="@string/title_activity_maps" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

I want to MainActivity be main, however when I delete line from MapsActivity :

<action android:name="android.intent.action.MAIN" />

and put it in the MainActivity - app doesn't work. Some problem with credentials I think - I see only white screen. Do you know why ?

This is my MainActivity :

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        setContentView(R.layout.activity_main);
        Log.e(TAG, "onCreate: " );
    }
}

Log is not being called so app doesn't enter to onCreate().

You need to move more than that 1 line - put all of this in the MainActivity element:

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

You need the filter and the category lines as well (this group makes it your main activity)

That's because there are two onCreate() methods:

void onCreate(Bundle)

void onCreate (Bundle savedInstanceState, PersistableBundle persistentState)

The second one will only be called if you set persistableMode attribute to persistAcrossReboots or if your activity is MAIN (root) activity.

You are using the second method without setting the attribute persistableMode . The default value of this attribute is persistRootOnly . Which means :

The default. If this activity forms the root of a task then that task will be persisted across reboots but only the launching intent will be used. If the task relinquishes its identity then the intent used is that of the topmost inherited identity. All activities above this activity in the task will not be persisted. In addition this activity will not be passed a PersistableBundle into which it could have stored its state.

Hence it worked for you when the maps activity was defined as the MAIN activity. But now that it is neither the root activity nor the persistableMode attribute is set to persistAcrossReboots , your onCreate(Bundle savedInstanceState, PersistableBundle persistentState) is never called.

SOLUTION:

Simply use the first onCreate(Bundle) method if you are not using the persistentState . If you are using persistentState then simply set the persistableMode attribute to persistAcrossReboots .

Related Android Developer Links -

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