Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Activity Keeps Resuming Saved Instance State
    primarykey
    data
    text
    <p>So this application consists on one 2 activities. The first asks the user for specifications on making a notification. On the button click the activity creates the notification. The code for this activity is below:</p> <pre><code>package com.dewey.notifymanager; import java.util.Random; import android.os.Bundle; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.support.v4.app.NotificationCompat; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Button button = (Button) findViewById(R.id.button1); final TextView texty = (TextView) findViewById(R.id.textView2); final EditText input = (EditText) findViewById(R.id.editText1); final EditText input2 = (EditText) findViewById(R.id.editText2); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { String notificationmessage = input.getText().toString(); String notificationdetails = input2.getText().toString(); texty.setText("Notification Created"); displayNotification(notificationmessage, notificationdetails); } }); } @Override protected void onStart() { super.onStart(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } public void displayNotification(String msg,String details) { NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.ic_launcher, msg, System.currentTimeMillis()); Random generator = new Random(); int i = 90686958 - generator.nextInt(92938); Intent intent = new Intent(this, ResultActivity.class); intent.putExtra("details", details); intent.putExtra("msg", msg); intent.putExtra("id", i); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); notification.setLatestEventInfo(this, msg, details, pendingIntent); notification.flags = notification.FLAG_ONGOING_EVENT; manager.notify(i, notification); } } </code></pre> <p>When the notification is clicked a new Activity is launched and some data is passed along in the intent, including the notification ID. The second activity then displays the data the notification contained, and it also has a button. When the user clicks this button the notification is removed. The code for the second activity is below:</p> <pre><code>package com.dewey.notifymanager; import android.os.Bundle; import android.app.Activity; import android.app.NotificationManager; import android.content.Context; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.TextView; public class ResultActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(null); setContentView(R.layout.activity_result); String details = getIntent().getExtras().getString("details"); String msg = getIntent().getExtras().getString("msg"); final int id = getIntent().getExtras().getInt("id"); TextView title = (TextView)findViewById(R.id.title); TextView descrip = (TextView)findViewById(R.id.details); title.setText(msg); descrip.setText(details); Button exitButton = (Button)findViewById(R.id.exitButton); exitButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.cancel(id); finish(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_result, menu); return true; } } </code></pre> <p>When I run the app it works oddly. When I create a notification and then click on it it launches the second activity and displays the correct data and the finish button rids the notification off the status bar. But then when I create a second notification and click on it, it launches the second activity, but displays the old data that the first application contained. I need to prevent this from happening.</p> <p>I think so far that the problem lies with the fact that my activity is resuming savedInstanceState, but I set it to 'null' in the second activity. What exactly is my problem? </p> <p>Is it even a problem regarding the activity resuming savedInstanceState or is it my logic? </p> <p>Thanks for the help.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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