简体   繁体   中英

android switch from one activity to another

I have two layout xml files, each with its own separate Activity class. Each class is valid, because I can refer to each activity from the application section of the manifest file and it launches and works. My problem is when I click a button on the first layout xml file to go to the other one - it will switch to the second layout xml file, but any actions on that layout do nothing. The code in the second activity class doesn't fire. For example, when I have MenuActivity listed first, it will display the layout xml file, and all calls on that layout work. When I click the button to switch to Home, it will display the home layout xml file, but all code within HomeActivity does nothing. I'm sure it's something simple, but I can't put my finger on it. Thanks in advance.

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MenuActivity"
        android:label="@string/title_activity_menu" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".HomeActivity" />
</application>

It sounds like you are using setContentView() instead of Intents to switch Activities.

For example, to launch your MenuActivity from your HomeActivity :

Intent menuIntent = new Intent(this, MenuActivity.class);
startActivity(menuIntent);

setContentView() simply changes the display layout; it does not create a new Activity.

Use Intent class to switch from one activity to another. Suppose your first activity is HomeActivity and you want to go to MenuActivity just write a simple 2 line code..

Intent intent=new Intent(HomeActivity.this,MenuActivity.class);

startActivity(intent);

I hope this works.

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