Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Blog App, should I use Sharedpreferences or Intents?

I'm building an android app, the followed pages of a current user are clickable, I save the clicked page id and pass it to open the corresponding page, once opened, the user can check the followers list, the posts inside the page..etc.

Here's the root node that contains all the page childs (database Firebase structure):

pageData

      id1
         +page_id
         +page_name
         +page_posts
            -001
               -post_id
               -contents
               ....

userData
      -001
         -user_id
         -user_name
         ....

followData
      -001 
        -page_id
        -follow_id
        -user_id

What is the best way to pass the page id between (followersListActivity, PostsActivity, AboutPageActivity)?

I read this https://github.com/flutter/flutter/issues/15307 , it's saying not to use shared preferences, I already used intents to pass the value, and retrieve it inside the onCreate(), but when the activity state changes (onStart() the page_id is null), I'm using for navigation a bottom nav menu.


Edit solution found : Jeff Gilfelt Thank you Android global variable

like image 261
seekerGS Avatar asked Dec 20 '25 09:12

seekerGS


1 Answers

It is correct that you shouldn't abuse sharePreferences for passing data between activities, and you could always save your data with onSaveInstanceState method:

@Override
protected void onSaveInstanceState(Bundle outState) {
  super.onSaveInstanceState(outState);
  outState.putString("page_id", pageID);
}

and retrieve it from onRestoreInstanceState or onCreate:

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  pageID = savedInstanceState.getString("page_id");
}

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  if (savedInstanceState!=null)
    pageID = savedInstanceState.getString("page_id");
}
like image 68
Teresa Avatar answered Dec 22 '25 00:12

Teresa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!