How to switch between views in android?

Posted by aurezza on Game Development See other posts from Game Development or by aurezza
Published on 2012-09-28T01:22:23Z Indexed on 2012/09/28 3:50 UTC
Read the original article Hit count: 420

Filed under:
|

I've tried several methods to switch between two views in my program. I've tried creating a new thread then have the view run for 5 seconds before creating intent to start my main activity. This is the code snippet from the said view class:

mHelpThread =  new Thread(){
        @Override
        public void run(){

            try {
                synchronized(this){
                    // Wait given period of time or exit on touch
                    wait(5000);

                }
            }
            catch(InterruptedException ex){                    
            }

            finish();

            // Run next activity
            Intent intent = new Intent(Intent.ACTION_MAIN, null);
            intent.addCategory(Intent.CATEGORY_HOME);
            startActivity(intent);
            //stop();                    
        }
    };

    mHelpThread.start();

I can access the said view without error but it doesn't disappear after 5 seconds nor did it switched to main view when I even utilized an onTouchEvent() to detect touch on the screen of which it should have automatically closed. I've also tried adding a button on the said view to manually switch to main view:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.help);
    final HelpView helpView = this;  
    final Button btnback = (Button) findViewById(R.id.back);
    btnback.setOnClickListener(new View.OnClickListener(){  
        public void onClick(View v) {
            Intent intent = new Intent(helpView, MainActivity.class);
            startActivity(intent);
        }
    });

} 

These codes worked, though, for creating a launcher for my program. So I thought that it would work the same if I added an option for help/rules(for the game) that would switch to another view. I've only since started using eclipse for android so pardon my lack of knowledge. Here is also the snippet from my manifest:

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="MainActivity"
        android:label="@string/title_activity_main">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
        <intent-filter></intent-filter>
    </activity>
    <activity android:name="SplashScreen"
              android:theme="@style/Theme.Transparent">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity android:name="HelpView"
              android:theme="@style/Theme.Transparent">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
        <intent-filter></intent-filter>
    </activity>
</application>

© Game Development or respective owner

Related posts about android

Related posts about view