Android activity of open dialogue without opening the main activity behind it

I am writing a program that offers a quick response dialog when receiving SMS.

However, I get an unexpected result. When I receive an SMS message, the corresponding dialog action appears with the correct phone number and message displayed, however it is followed by the second action, which is the default activity in my program (this is what opens when my application starts)

I do not want this second action to appear. The quick response operation should occur on its own over what happened before.

Floating activity:

public class quickReply extends Activity { String mNumber, mMessage; TextView mMainText; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mMainText = (TextView)findViewById(R.id.mainText); try{ Intent i = getIntent(); Bundle extras = i.getExtras(); mNumber = extras.getString("theNumber"); mMessage = extras.getString("theMessage"); this.setTitle("Message From:" + mNumber); mMainText.setText(mMessage); } catch(Exception e) { mMainText.setText(e.getMessage()); } } 

}

Activity call inside onReceive ()

  Intent i = new Intent(context, quickReply.class); i.putExtra("theNumber", mNumber); i.putExtra("theMessage", mMessage); i.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); 

Manifesto:

 <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".quickReply" android:label="@string/app_name" android:theme="@android:style/Theme.Dialog" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".SmsReceiver"> <intent-filter> <action android:name= "android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver> </application> 

+6
android android-activity dialog
source share
2 answers

the only way I found that works is in defining your activity in the manifest:

 android:launchMode="singleInstance" 

but then you should resume your main default activity after rejecting the dialog. NOTE: you will lose all state from the previous launch, so this is not an ideal solution.

UPDATE:

You can also do this:

 Intent.FLAG_ACTIVITY_CLEAR_TASK 

so here is what i did:

  • open source / main action.
  • from the service, start the dialog style action using the above (main by bye-bye).
  • when the user rejects the dialog, run main again with an additional intent (IS_BACK), which is processed in onCreate () and calls:

    moveTaskToBack (true);

this will cause the task to be in the dialog box at the top and at the top of the stack.

+3
source share

You must establish the proximity of the operation to something other than your main activity. This will separate it from the main action and will track it as a separate task:

 <activity android:name=".quickReply" android:label="@string/app_name" android:theme="@android:style/Theme.Dialog" android:launchMode="singleTask" android:taskAffinity="quickReply" > 
0
source share

All Articles