Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to start an Activity from Application class - Android

Tags:

android

I am trying to access if user is logged in or not, based on that I try to start activity for the application. While doing this, the app crashes in the Application class.

The code for application class is

public class MyClass extends Application {
    public static Context context;

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();

        if(ParseUser.getCurrentUser().isAuthenticated()){
            startActivity(new Intent(context, MainActivity.class));
        }

    }
}

Any suggestion why this happens ?

In my Manifest I have declared the intent filter for another activity.

 <activity
            android:name=".Activities.Register"
            android:label="@string/title_activity_register" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
like image 713
Shaik MD Ashiq Avatar asked Jul 05 '15 13:07

Shaik MD Ashiq


People also ask

How do you launch an activity from an application?

Call the startActivity() method with the new intent as the argument. startActivity(intent); Run the app. When you click the Send button the main activity sends the intent and the Android system launches the second activity.

How do I launch an activity using intent?

To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo.

What is Flag_activity_new_task in Android?

The flags you can use to modify the default behavior are: FLAG_ACTIVITY_NEW_TASK. Start the activity in a new task. If a task is already running for the activity you are now starting, that task is brought to the foreground with its last state restored and the activity receives the new intent in onNewIntent() .

How do you start a activity from a non activity class?

public Context call mcontext;<br> // ..... <br> Intent intent = new Intent(call mcontext, AboutActivity. class);<br> call mcontext. startActivity(intent);


1 Answers

Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Also if you look at Logcat in Android Studio, you should be seeing error log that has already told you how to fix this issue.

like image 106
Nicholas Ng Avatar answered Oct 05 '22 05:10

Nicholas Ng