Android:不赞成使用getIntent() [英] Android: getIntent() is deprecated

查看:503
本文介绍了Android:不赞成使用getIntent()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序由MainActivity和两个片段活动组成.我需要一个片段来从用户那里获取一个String值,并将其传递给第二个片段.

My program consists of a MainActivity and two fragment activities. I need one fragment to take a String value from the user and pass it to the second fragment.

我正在设法解决这个问题.由于我熟悉意图,因此在另一篇文章中找到了此答案,并决定尝试一下.一切顺利,直到进入第4步,当我尝试在第二个片段中使用Intent i = getIntent();时,Studio不允许我使用它并说"getIntent(java.lang.String) is deprecated".

I am trying to wrap my head around how to do this. Since I am familiar with intents, I found this answer on another post and decided to try it out. Everything looks fine until I get to step 4, when I try to use Intent i = getIntent(); in my second fragment, Studio won't let me use it and says "getIntent(java.lang.String) is deprecated".

这对我来说没有意义,因为我在其他程序中使用getIntent()没问题,这让我可以在我的MainActivity(另一篇文章的第2步)中使用它,而无需大吼大叫.

This doesn't make sense to me since I have used getIntent() in other programs without issue, and it is letting me use it in my MainActivity (step 2 from the other post) without screaming at me.

我知道不使用意图就可以做到这一点,但是我无法弄清楚,也找不到任何真正详尽的教程来做到这一点.所以我想我的问题是:

I know this can be done without using intents, but I can't figure it out and can't find any really thorough tutorials in order to do so. So I guess my questions are:

  1. 我是否可以为此目的继续工作?我应该怎么做才能解决这个过时的问题?
  2. 任何其他建议,解释或指向像我5岁时一样解释"教程的链接都将非常有帮助并受到欢迎.我已经在Google上搜索并阅读了一些文章,但我仍然对此不了解,并且越来越感到沮丧.看来这应该是一个相对简单的概念.

推荐答案

现在回答还为时已晚,但我仍在为其他人提供答案.之所以发生这种情况,是因为Intent基本上是与活动一起使用的.碎片不是活动,而是依附于活动.因此,您只需执行以下操作:

It is too late for answer but still I am providing my answer for other persons. It is happen because Intent is basically work with an activity. And fragments are not activity, but attached to activity. So simply you need to do this:

Intent intent=getActivity().getIntent();

这篇关于Android:不赞成使用getIntent()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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