Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling startActivity() from outside of an activity context requires the FLAG_ACTIVITY_NEW_TASK

Tags:

android

I am trying to start an activity inside a service class. I have a following code:

public class SendLinkService extends Service {

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Bundle bundle = intent.getExtras();
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_TEXT, bundle.getString("URL"));
    getApplicationContext().startActivity(Intent.createChooser(shareIntent, "Share via"));
    return super.onStartCommand(intent, flags, startId);
}
}

It gives exception on following line of onStartCommand() :

getApplicationContext().startActivity(Intent.createChooser(shareIntent, "Share via"));
like image 418
Vikalp Avatar asked Apr 20 '14 13:04

Vikalp


People also ask

What is startActivity ()?

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?

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() .

Can I start activity with application context?

The main difference between Application and Activity Context is that Application Context is not related to UI. That means we shouldn't use application context for inflate a layout, start activity nor show an dialog.


2 Answers

@hariharan answer works. However it also work without setting Intent.FLAG_ACTIVITY_NEW_TASK in the first case. More accurate answer is:

Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, bundle.getString("URL"));
Intent new_intent = Intent.createChooser(shareIntent, "Share via");
new_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
getApplicationContext().startActivity(new_intent);
like image 149
vedi Avatar answered Oct 19 '22 11:10

vedi


For (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) it is mandatory to add Intent.FLAG_ACTIVITY_NEW_TASK while calling startActivity() from outside of an Activity context.

Docs

Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, bundle.getString("URL"));
Intent new_intent = Intent.createChooser(shareIntent, "Share via");
new_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
getApplicationContext().startActivity(new_intent);
like image 27
Hrishikesh Kadam Avatar answered Oct 19 '22 10:10

Hrishikesh Kadam