Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. final Intent activityIntent = new Intent(context, MainActivity.class);
  2. activityIntent.putExtra("smsChallenge", smsText);
  3. activityIntent.putExtra("smsSenderNumber", senderMobilNumber);
  4. activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  5. activityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  6. context.startActivity(activityIntent);
  7.  
  8. Bundle extras = getIntent().getExtras();
  9. if (extras != null) {
  10. smsChallenge = extras.getString("smsChallenge");
  11. extras.remove("smsChallenge");
  12. }
  13.  
  14. getIntent().removeExtra("smsChallenge");
  15.  
  16. public YourActivity extends Activity {
  17.  
  18. private boolean extrasClearedOut;
  19. private SharedPreferences mPrefs;
  20. private static String EXTRA_CLEAR_OUT = "extras_cleared_out";
  21.  
  22. public void onCreate(Bundle savedInstanceState) {
  23. mPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
  24.  
  25. extrasClearedOut = mPrefs.getBoolean(EXTRA_CLEAR_OUT, false);
  26.  
  27. if (savedInstanceState != null && savedInstanceState.getBoolean(EXTRA_CLEAR_OUT, false)) {
  28. extrasClearedOut = true;
  29. }
  30.  
  31. Intent intent = getIntent();
  32. if (extrasClearedOut) {
  33. // ignore the extras in the intent.
  34.  
  35. }
  36.  
  37. else {
  38. // Read and use the extras in the intent.
  39. }
  40. }
  41.  
  42. protected void onNewIntent (Intent intent) {
  43. super.onNewIntent(intent);
  44. setIntent(intent);
  45.  
  46. if (extrasClearedOut) {
  47. // ignore the extras in the intent.
  48. }
  49. else {
  50. // Read and use the extras in the intent.
  51. }
  52. }
  53.  
  54. public void someMethod(...) {
  55. extrasClearedOut = true;
  56. }
  57.  
  58. protected void onSaveInstanceState (Bundle outState) {
  59. super.onSaveInstanceState(outState);
  60. outState.putBoolean(EXTRA_CLEAR_OUT, extrasClearedOut);
  61. }
  62.  
  63. public void onDestroy()
  64. {
  65. final Editor edit = mPrefs.edit();
  66. edit.putBoolean(EXTRA_CLEAR_OUT, extrasClearedOut);
  67. edit.commit();
  68. }
  69.  
  70. }
  71.  
  72. private void removeExtraFromBundle(String key){
  73. Bundle bundle = getArguments();
  74. if(bundle != null){
  75. String value = null;
  76. value = bundle.getString(key);
  77. if(value != null){
  78. bundle.remove(key);
  79. }
  80. }
  81. }
  82.  
  83. final Intent activityIntent = new Intent(context, MainActivity.class);
  84. activityIntent.putExtra("smsChallenge", smsText);
  85. activityIntent.putExtra("smsSenderNumber", senderMobilNumber);
  86. activityIntent.putExtra("nonce", UUID.randomUUID().toString);
  87. activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  88. activityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  89. context.startActivity(activityIntent);
  90.  
  91. public void onCreate(final Bundle savedInstanceState) {
  92. super.onCreate(savedInstanceState);
  93. Intent = getIntent();
  94. smsChallenge = intent.getStringExtra("smsChallenge", null);
  95. String nonce = intent.getStringExtra("nonce", null);
  96. if (nonce == null) {
  97. // should not happen, but do something sane here
  98. }
  99. SharedPreferences sp = PreferenceManager.getDefaultPreferences(this);
  100. if (nonce.equals(sp.getString("nonce", null)) {
  101. // app was restarted
  102. } else {
  103. // app was started from a shiny fresh intent
  104. sp.edit().putString("nonce", nonce).commit();
  105. }
  106. }
  107.  
  108. final Intent activityIntent = new Intent(context, MainActivity.class);
  109.  
  110. final Intent activityIntent = new Intent(context.getApplicationContext(), MainActivity.class);
  111.  
  112. public void onCreate(Bundle savedInstanceState) {
  113. super.onCreate(savedInstanceState);
  114.  
  115. // The Intent provided by getIntent() (and its extras) will persist through a restore
  116. // via savedInstance. Because of this, restoring this activity from a
  117. // an instance that was originally started with extras (deep-link or
  118. // pre-defined destination) may cause un-desired behavior
  119. // (ie...infinite loop of sending the user directly to somewhere else because of a
  120. // pre-defined alternate destination in the Intent's extras).
  121. //
  122. // To get around this, if restoring from savedInstanceState, we explicitly
  123. // set a new Intent *** to override the original Intent that started the activity.***
  124. // Note...it is still possible to re-use the original Intent values...simply
  125. // set them in the savedInstanceState Bundle in onSavedInstanceState.
  126. if (savedInstanceState != null) {
  127. // Place savedInstanceState Bundle as the Intent "extras"
  128. setIntent(new Intent().putExtras(savedInstanceState));
  129. }
  130.  
  131. processIntent(getIntent())
  132. }
  133.  
  134. private void processIntent(Intent intent) {
  135. if (getIntent().getExtras() == null) {
  136. // Protection condition
  137. return;
  138. }
  139.  
  140. doSomething(intent.getExtras.getString("SOMETHING_I_REALLY_NEED_TO_PERSIST"));
  141.  
  142. final String somethingIDontWantToPersist =
  143. intent.getExtras.getString("SOMETHING_I_DONT_WANT_TO_PERSIST");
  144.  
  145. if(somethingIDontWantToPersist != null) {
  146. doSomething(somethingIDontWantToPersist);
  147. }
  148. }
  149.  
  150. @Override
  151. public void onSaveInstanceState(Bundle savedInstanceState) {
  152. // Save selective extras from original Intent...
  153. savedInstanceState.putString("SOMETHING_I_REALLY_NEED_TO_PERSIST", "persistedValued");
  154. super.onSaveInstanceState(savedInstanceState);
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement