nasif
nasif

Reputation: 137

Threading is not working

I want to show my splash.xml for 6 seconds and then activity_main.xml but it is not showing the activity_main.xml...

my splash.java code:

    package com.example.ne;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;

    public class splash extends Activity{

     protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.splash);
            Thread timer = new Thread()
            {
                public void run(){
                    try{
                        sleep(6000);


                    } catch(InterruptedException e){
                        e.printStackTrace();

                    }finally{
                        Intent open =new     Intent("com.example.ne.MAINACTIVITY");
                        startActivity(open);
                    }
                }
            };
            timer.start();

     }


}

and mainactivity.java code:

    package com.example.ne;

    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;

    public class MainActivity extends Activity {


    int counter;
    Button add,sub;
    TextView display;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        counter=0;
        add=(Button) findViewById(R.id.bAdd);
        sub=(Button)findViewById(R.id.bSub);
        display=(TextView)findViewById(R.id.tvdisplay);
        add.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View arg0) {
                counter++;
                // TODO Auto-generated method stub
                display.setText("Your total is" + counter);

            }
        });sub.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View arg0) {
                counter--;
                display.setText("Your total is" + counter);
                // TODO Auto-generated method stub

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

and manifest.xml code:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ne"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.ne.splash"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
 <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity" android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.ne.MAINACTIVITY" />

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

</manifest>

Upvotes: 1

Views: 74

Answers (3)

zgc7009
zgc7009

Reputation: 3389

Not that you can't do it your way, but managing threads is trickier business than it may seem. That is why Android incorporated a number of helpers for it. In your case, you could use a Handler to post a delayed runnable that will act as your redirect.

new Handler().postDelayed(new Runnable(){
    @Override
    public void run(){
        Intent open = new Intent(splash.this, MainActivity.class);
        startActivity(open);
        splash.this.finish();
    }
}, 6000);

This will keep you from having to manage any threads and will close the splash activity after the intent is passed.

Second, if you will notice, the Intent instantiation, new Intent(splash.this, MainActivity.class), this is the way you should create Activity intents. This will provide context for the explicitly called intent.

Third, the class splash should be Splash, per Java naming conventions. Camel case is appropriate, but classes have an uppercase first letter, while methods and variables have a lowercase first letter.

And last, your manifest should look like this

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.ne"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".splash"
            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=".MainActivity" 
            android:label="@string/app_name" />
    </application>
</manifest>

EDIT

As recommended by @JHH it is also possible to stop the splash page from redirecting on things like back pressed or the home screen. What you can do for that is:

First, make the class variables mHandler and mRunnable and when you instantiate your objects, let the variables reference them.

private Handler mHandler;
private Runnable mRunnable;

@Override
public void onCreate(Bundle savedInstanceState){
    ...
    mHandler = new Handler();
    mRunnable = new Runnable(){...};
    ...
}

Next, let the handler post the runnable message with a delay in onResume. This will allow us to create the post message every time the splash activity comes to the front (remember that after the redirect the splash screen will finish so this won't be an issue).

@Override
public void onResume(){
    super.onResume();
    postDelayed(mRunnable, 6000);
}

Lastly, in onPause we can then stop the delayed post like so. This will keep the handler from triggering the intent which will make it so the redirect doesn't happen if the splash screen is in the background. In other words, if you hit the splash screen then hit the back/home button the runnable won't continue and open the activity anyway.

@Override
public void onPause(){
    mHandler.removeCallbacks(mRunnable);
    super.onPause();
}

Upvotes: 2

Zeeshan Shabbir
Zeeshan Shabbir

Reputation: 7114

Make following changes in your code

Package com.example.ne;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class splash extends Activity{

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        Thread timer = new Thread()
        {
            public void run(){
                try{
                    sleep(6000);


                } catch(InterruptedException e){
                    e.printStackTrace();

                }finally{
                    Intent open =new Intent(splash.this,MainActivity.class);
                    startActivity(open);
                }
            }
        };
        timer.start();

   }

   }

Upvotes: 0

Ali Elgazar
Ali Elgazar

Reputation: 777

Try instead

Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);

Upvotes: 0

Related Questions