Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extremely basic: Switching between Activities (Android)

Hey, I realize there are tutorials on this topic, and even previous questions posed. However, I've read several tutorials and some answers and I am still having trouble. Clearly, I must not be the brightest crayon in the box.

My program crashes when I try to switch between activities with the following code:

    final Button switchButton = (Button) findViewById(R.id.change_mode);
    switchButton.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            Intent runOptionSelect = new Intent(TheDecider.this, OptionSelect.class);
            startActivity(runOptionSelect);
            return;
        }
    });

I think this code is fine so it must be an issue with the manifest.xml right? I don't understand when to use which activity constant. If my purpose is simply to switch to a different layout and class, what should I choose?

Also, are MAIN and LAUNCHER only to be used on the initial activity to be run?

So sorry for asking such a basic question but I've spent far too much time researching this to no avail. Thank you.

like image 774
BlackVegetable Avatar asked May 18 '11 17:05

BlackVegetable


People also ask

What is used to navigate between activities in android?

Intents: Navigating between Activities (Amongst Other Things). We mentioned earlier that navigation between activities is managed by Intents. An Intent is a type of message that applications broadcast through the Android OS to interested parties on the phone.


2 Answers

Please check below code in your manifest.xml file


<activity android:name=".TheDecider"
              android:label="@string/app_name"
              >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
 <activity android:name=".OptionSelect"
              android:label="@string/app_name"
              >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

like image 188
Nikhil Avatar answered Oct 26 '22 14:10

Nikhil


In the intent, the first parameter is the current context (you can do

TheDecider.this

or

getApplicationContext() 

there) and the second one is the class from the activity you are trying to reach.

You are doing that right. And in your manifest you should add

<activity android:name=".OptionSelect"
    android:label="@string/app_name" />

You have to add EVERY Activity in your Manifest, otherwise it will crash. Without knowing your logcat's content, that's all i can say.

like image 45
ferostar Avatar answered Oct 26 '22 15:10

ferostar