Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Just to make it clear (because I used a lot of time figuring out how to make it work)</p> <p>In the service class that extends <code>BroadcastReceiver</code>. Put in the following code in <code>onReceive()</code></p> <pre><code>Intent intent2open = new Intent(context, YourActivity.class); intent2open.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent2open.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); String name = "KEY"; String value = "String you want to pass"; intent2open.putExtra(name, value); context.startActivity(intent2open); </code></pre> <p>The <code>FLAG_ACTIVITY_SINGLE_TOP</code> makes sure the apps doesn't re-open if already open. This means that the "old" intent that opened YourActivity in the first place is re-used and it will NOT contain the extra values. You have to catch them in another method called <code>onNewIntent()</code> in <code>YourActivity.</code></p> <pre><code>public class YourActivity extends Activity { private String memberFieldString; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Code doing your thing... } // End of onCreate() @Override protected void onNewIntent(Intent intent) { Log.d("YourActivity", "onNewIntent is called!"); memberFieldString = intent.getStringExtra("KEY"); super.onNewIntent(intent); } // End of onNewIntent(Intent intent) @Override protected void onResume() { if (memberFieldString != null) { if (opstartsIntent.getStringExtra(KEY) != null) { Log.d("YourActivity", "memberFieldString: "+ memberFieldString); } else { Log.d("YourActivity", "The intent that started YourActivity did not have an extra string value"); } } } // End of onResume() } // End of YourActivity </code></pre> <p>Please note the two if statements - the <code>onResume()</code> does not know if it's called after <code>OnCreate()-&gt;OnStart() OR onRestart()-&gt;onStart()</code><br> <strong>Please see:</strong> <a href="http://www.anddev.org/images/android/activity_lifecycle.png" rel="noreferrer">http://www.anddev.org/images/android/activity_lifecycle.png</a></p> <p>It's just used to test if the app is launched by the user (intent with no extras) OR by the <code>BroadcastReceiver</code> (intent with extras).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload