JSONParser
JSONParser

Reputation: 1132

Android manifest start activity

I have learnt that whichever activity contains .MAIN AND .LAUNCHER will be launched first whenever i click the app in my device and i have also learnt that order of activities does not matter. My app contains two activities first one .MainActivity and second one .MainActivity2Activity. I have written .MAIN AND .LAUNCHER in both of activities with .MainActivity before .MainActivity2Activity doing this the app started correctly but when i change the order then .MainActivity2Activity starts **first with fake data ("hello world appears from nowhere") and .MainActivity doesn't start(on clicking the back button in .MainActivity2Activity activity). My question is that , does order of activities matter and why it didn't showed any error that two activities contain .MAIN AND . LAUNCHER , AND FROM WHERE HELLO WORLD appears.

<activity
        android:name=".MainActivity"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat.Light.DarkActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".MainActivity2Activity"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/title_activity_main_activity2"
        android:theme="@style/Theme.AppCompat.Light.DarkActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.utkarsh.beatle.app.MainActivity" />

    </activity>

Upvotes: 0

Views: 3177

Answers (3)

Domain
Domain

Reputation: 11808

The xml launches the activity which it finds first and thus the one you wrote on top. For the hello world problem it might be that you had a textView whose original value might be hello world and you get some value from a intent call and replace it with that value. Hence when it is directly launched as main its value is Hello World.

The reason why it showed no error was that you can have 2 activities even if its not recommended. You just need to have this code in one of them in the xml.

android:taskAffinity="your.own.package.SecondActivity"

They should both get their separate icons so keep a different name or icon for them. Each one will launch a different activity. The back button will not take you home as you have directly launched on the second activity and have not been redirected there from the first activity.

Upvotes: 2

Bala Saikrupa Puram
Bala Saikrupa Puram

Reputation: 721

Launcher activity is the one which will execute when the application is launched if you give both the activities are launcher there is no meaning in that. And you need to use intent for switching between two activities, for better guidance post your code here....

this is the way of writing your code for 2 activities

<activity
        android:name=".MainActivity"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat.Light.DarkActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".MainActivity2Activity"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/title_activity_main_activity2"
        android:theme="@style/Theme.AppCompat.Light.DarkActionBar">

    </activity> 

Upvotes: 0

Sanjeev
Sanjeev

Reputation: 4375

To start another Activity from current activty you have to use Intent ,it will not open just by selecting back button, set LAUNCHER to which ever Activity you want to open first ,suppose MainActivity2 then inside your MainActivity2 write

public class MainActivity2 extends Activity(){
     protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ui);

    //Calling other activity
    Intent intent=new Intent(MainActivity2.this,MainActivity.class);
    startActvity(intent);
  }
}

Upvotes: 0

Related Questions