Android Blog App,我应该使用Sharedpreferences还是Intents? [英] Android Blog App, should I use Sharedpreferences or Intents?

查看:79
本文介绍了Android Blog App,我应该使用Sharedpreferences还是Intents?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个android应用,可点击当前用户的以下页面,保存点击的页面ID并将其传递以打开相应的页面, 打开后,用户可以查看关注者列表,页面内的帖子.等.

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.

这是包含所有页面子项(数据库Firebase结构)的根节点:

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

在之间(followersListActivity,PostsActivity,AboutPageActivity)传递页面ID的最佳方法是什么?

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

我阅读了此 https://github.com/flutter/flutter/issues/15307 ,这就是说不使用共享首选项,我已经使用了意图来传递值,并在onCreate()内部检索它,但是当活动状态更改时(onStart()page_id为null),我正在使用导航底部的导航菜单.

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.

编辑解决方案:Jeff Gilfelt谢谢 Android全局变量

Edit solution found : Jeff Gilfelt Thank you Android global variable

推荐答案

实际上,将参数传递给子活动和片段的标准方法是通过Intents.您可以传递任何原始值,任何原始对象的array [],一些ArrayLists<>,甚至任何Parcelable对象,

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

参数CODE(int)是您分配给activity的代码,以防万一它还返回值.然后,您可以在调用活动中的覆盖的onActivityResult()中检查返回值.如果您的活动未返回任何值,则该参数无关.

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.

在子活动中,您可以在onCreate(或其后的任意位置)中获取参数:

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

}

如果您想要然后将内容从子活动返回到父活动,则可以在finish()之前的子活动中使用setResult():

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();
}
.
.
.

然后在通话活动中:

.
.
.
@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")


     }  

}

这篇关于Android Blog App,我应该使用Sharedpreferences还是Intents?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆