Skip to content Skip to sidebar Skip to footer

Androidruntimeexception "calling Startactivity() From Outside Of An Activity Context Requires The Flag_activity_new_task Flag"

I create multiple layouts inside a listview, but when i click i get a AndroidRuntimeException 'Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVIT

Solution 1:

Replace getApplicationContext() with this. Most likely, you should do that everywhere in your code that you have getApplicationContext() -- only use getApplicationContext() when you specifically need the Application object.

Solution 2:

Old thread, but thought this easy solution might help someone. Instead of passing context around, just get it from view. For eg: view.getContext()

@OverridepublicvoidonClick(View view) {
    Bundle bundle=newBundle();
    bundle.putString("url", clickUrl);
    Intentintent=newIntent(view.getContext(),CustomWebView.class);
    intent.putExtras(bundle);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    view.getContext().startActivity(intent);
}

Solution 3:

I have added:

parent.getApplicationContext()

Instead of just:

getApplicationContext()

Whole line is:

retval=LayoutInflater.from(parent.getApplicationContext()).inflate(R.layout.layout_anuncio, null);

Solution 4:

The solution of Warren Lankie Van Tonder is almost the good:

-You should avoid using Activity context and choose Application Context instead, in purpose to prevent memory leaks. This blog of an Android developer explains that http://android-developers.blogspot.be/2009/01/avoiding-memory-leaks.html

-But in the case of Activity context is the only solution for your code (maybe for calling another activity from outside an activity) and regarding the link above, you have to release the static reference in each onPause() with

AppGlobals.setAppContext(null);

In return, set the static field in onResume and not onCreate.

Solution 5:

Thanks to CommonsWare i came up with this solution which worked best.

I added a new class

package com.test.test;

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

publicclassAppGlobalsextendsActivity {
    privatestatic Context appContext;

    publicstatic Context getAppContext(){
        return appContext;
    }

    publicstaticvoidsetAppContext(Context context){
        appContext = context;
    }

    publicstaticvoidStartActivity(Intent intent){
        appContext.startActivity(intent);
    }
}

All i needed to do then was add the below code on each activity onCreate that i navigated to.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    AppGlobals.setAppContext(this);
}

This allowed the new intent to start as if it working with normal application flow and this way i didn't have to set a Flag for the intent.

Calling the start activity method is as easy as:

Intent totalTimerIntent = newIntent(AppGlobals.getAppContext(), TotalTimer.class);
AppGlobals.StartActivity(totalTimerIntent);

Hope this helps someone as it has helped me.

Thank you CommonsWare.

Post a Comment for "Androidruntimeexception "calling Startactivity() From Outside Of An Activity Context Requires The Flag_activity_new_task Flag""