
我正在构建一个android应用,可点击当前用户的以下页面,保存点击的页面ID并将其传递以打开相应的页面,
打开后,用户可以检查关注者列表,页面内的帖子.
这是包含所有页面子项(数据库Firebase结构)的根节点:
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的最佳方法是什么?
我读到了https://github.com/flutter/flutter/issues/15307,它的意思是不使用共享首选项,我已经使用了意图来传递值,并在onCreate()内检索它,但是当活动状态更改时(onStart()page_ID为null),我用于导航底部的导航菜单.
找到编辑解决方案:Jeff Gilfelt谢谢
Android global variable
解决方法:
实际上,将参数传递给子活动和片段的标准方法是通过Intent.您可以传递任何原始值,原始对象的任何array [],某些ArrayLists甚至任何Parcelable对象,
我在代码中使用以下例程:
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);}然后,您可以这样称呼它:
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)是分配给活动的代码,以防万一它还返回值.然后,您可以在调用活动的onActivityResult()中检查返回的值.如果您的活动未返回任何值,则该参数无关紧要.
在子活动中,您可以在onCreate中(或其后的任何位置)获取参数:
@OverrIDepublic 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);}如果要然后将内容从子活动返回到父活动,则可以在完成()之前在子活动中使用setResult():
. (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();}...然后在调用活动中:
...@OverrIDepublic 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?全部内容,希望文章能够帮你解决Android Blog App,我应该使用Sharedpreferences还是Intents?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)