Implicit and explicit intent in Android (crash startActivity (intent))

I am new to this world. I have a problem when I use startActivity (intent). This is the manifesto:

<activity
        android:name="com.example.counter.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="com.example.counter.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

And this is the code:

 public class Splash extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.splash);

    Thread timer = new Thread(){
        public void run()
        {
            try
            {
                sleep(5000);

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

                Intent i=new Intent ("com.example.counter.MainActivity");
                startActivity(i);
            }
        }
    };

    timer.start();

}

I would like to show Splash activity for 5 seconds and then show MainActivity. LogErrors :! https://www.dropbox.com/s/kg7xyp6h4b95itq/Screenshot%202014-02-08%2016.57.36.png

+6
source share
6 answers

There are two ways to do what you are trying to do.

  • Using Implicit Intent
  • Using explicit Intent

Contact Types of Intent

  • Implicit Intent

Intent Filters Activity AndroidManifest.xml. , Android , Intents ( MainActivity).

    <activity
    android:name="com.example.counter.MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="com.example.counter.MainAction" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    <activity>

Activity Intent

Intent i=new Intent ("com.example.counter.MainAction");
startActivity(i);

Intents , , , , Android , . , Intent, .

. , , . startActivity(). , startActivity() , , , . resolActivity() .

  1. Intent

Intent, , Activity . Intent, (Activity), .

  Intent i=new Intent (this,MainActivity.class);
  startActivity(i);
+14

class, . , - :

Intent newAct = new Intent(this, Splash.class);
startActivity(newAct);

, , Action, .

+3

, Splash - Launcher, :

<activity
    android:name="com.example.counter.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="com.example.counter.MainActivity"
    android:label="@string/app_name" >
</activity>

:

public class Splash extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        /*Splach screen that display for 5 seconds when app start*/
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent i = new Intent(Splash.this, MainActivity.class);
                startActivity(i);
                finish();
            }
        }, 5000);

    }

}

, .

+1

, ( ):

Intent i=new Intent ("com.example.counter.MainActivity");

( ):

startActivity(new Intent(mContext, MainActivity.class));

action intent filter MainActivity:

<action android:name="android.intent.action.MAIN" />

<action android:name="com.example.counter.MainActivity"/>
+1

You need to declare an action in the manifest file.

Like this:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".FirstActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activty
        android:name="com.example.counter.MainActivity"/>
</application>

Hope this helps.

0
source

I think it's better if you use Handler to put this code in pop-up activity in onCreate

new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(intent);
                finish();
            }
        }, 1500); 

and if you want to open it once, it is useful to use SharedPreferences

0
source

Source: https://habr.com/ru/post/1525944/


All Articles