简体   繁体   中英

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

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");
}

Actually the standard way of passing parameters to child activities and fragments is through Intents . You can pass any primitive value, any array[] of primitive objects, some ArrayLists<>, and even any Parcelable object,

I use the following routine in my code:

public static void runChildActivity(@NonNull Activity context, int code, @NonNull Class<? extends Activity> activityClass, @Nullable Bundle parameters) {
        Intent intent = new Intent(context, activityClass);
        if (parameters != null) intent.putExtras(parameters);
        context.startActivityForResult(intent, code);
}

And then, you can call it like this:

Bundle parameters = new Bundle();
parameters.putString("page_id", xxxx);
parameters.putFloat("temperature", 24.5f);
parameters.putParcelable("complexobject", yourParcelableObject);

runChildActivity(context, CODE, PostsActivity.class, parameters);

The parameter CODE (int) is a code you assign to your activity just in case it also returns values. Then you can check for the returning values in an overridden onActivityResult() in the calling activity. If your activity does not return any value, the parameter is irrelevant.

In the child activity, you can get the parameters in onCreate (or anywhere after it):

@Override
public void onCreate(Bundle savedInstanceState) {

    Bundle parameters = getIntent().getExtras();

    // in a fragment it'd be
    // Bundle parameters = getArguments();

    // those are the parameters passed. Second value is default
    // value in case parameter is not found.

   String pageId = parameters.getString("page_id", "-1");
   float temperature = parameters.getFloat("temperature", 0f);

}

If you want then to return stuff from your child activity back to the parent activity, you then use setResult() in the child activity before finish() :

. (child activity)
.
.
private void finishWithParameters() {

    Bundle parameters = new Bundle();
    parameters.putString("returnvalue", "user is crazy");
    parameters.putInteger("filesdownloaded", 43);
    Intent i = new Intent();
    i.putExtras(parameters);
    setResult(Activity.RESULT_OK, i);
    finish();
}
.
.
.

And then in the calling activity:

.
.
.
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {

     if (requestCode == CODE && data != null) {
        // that's the CODE we ran runChildActivity() with
        // so stuff is coming from that child activity

        Bundle parameters = data.getExtras();
        int filesDownloaded = parameters.getInt("filesDownloaded")

        // ... or directly, without even getting the bundle ....

        int filesDownloaded2 = data.getIntExtra("filesDownloaded")


     }  

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM