-5

I send FCM push notifications to my android app and when i click on the intent i get the message displayed in My Activity. After viewing the message, when the Activity is destroyed or killed the message is no longer display in my activity. I want to save the message in SharedPreferences so that i can view it whenever i resume that activity.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_result);
    String title = getIntent().getStringExtra("title");
    String message = getIntent().getStringExtra("message");

    setTitle(title);
    TextView desc = (TextView) findViewById(R.id.desc1);
    desc.setText(message);
}

1 Answers1

0

I don't see why you would like to do that, but sure. If saving to SharedPrefs is all you want, it's quite straight forward

SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("yourMessageKey", message);
editor.commit();

And of course, to read it back you

SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
String message = sharedPref.getString("yourMessageKey", defaultValue);
Algar
  • 5,734
  • 3
  • 34
  • 51