1

I use this code to dynamically create an activity from a service:

Manifest:

<activity android:name="ServiceDialog" android:label="" android:theme="@android:style/Theme.Dialog" />

Activity class:

public class ServiceDialog extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent=getIntent();
        String text = "";
        if(intent.hasExtra("text")) text = intent.getStringExtra("text");

        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle("Alert");
        alert.setIcon(android.R.drawable.ic_dialog_info);
        alert.setMessage(text);
        alert.setPositiveButton(android.R.string.ok,
                new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ServiceDialog.this.finish();
                    }
                });
        alert.setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface arg0) {
                ServiceDialog.this.finish();
            }
        });
        alert.show();
    }

}

Activity creating:

Intent intent = new Intent("android.intent.action.MAIN");
intent.setClass(this, ServiceDialog.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("text", "Hello!");
startActivity(intent);

But I have a problem - this activity isn't showing in task manager (if I press the "Home" button, then I can't reopen the activity). What can I do to fix it?

woggles
  • 7,444
  • 12
  • 70
  • 130
BArtWell
  • 4,176
  • 10
  • 63
  • 106
  • Delete this line intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); – Pasha Aug 30 '12 at 12:46
  • It's doesn't working and I get this error in LogCat: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag – BArtWell Aug 31 '12 at 07:26
  • maybe this can help you http://stackoverflow.com/questions/3689581/calling-startactivity-from-outside-of-an-activity – Pasha Aug 31 '12 at 08:23
  • Try flag setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK); – Pasha Aug 31 '12 at 08:24

1 Answers1

0

use this code instead to start your activity.

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.putExtra("text", "Hello!");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setComponent(new ComponentName("com.package.address","com.package.address.MainActivity"));
startActivity(intent);
Eldhose M Babu
  • 14,382
  • 8
  • 39
  • 44
  • This code doesn't working and I get this error in LogCat: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. If I set this flag there is no error, but it also doesn't working (alert doesn't showing). – BArtWell Aug 31 '12 at 07:33