Android 在其他活动中使用getIntent()访问时的相同项值
我的代码流是:Android 在其他活动中使用getIntent()访问时的相同项值,android,android-intent,oncreate,Android,Android Intent,Oncreate,我的代码流是: Reports=>ReportsType 报表有3个项目,单击每个项目时,我将启动活动ReportsType传递带有名称name的标记,以区分单击了哪个项目 问题是OnCreate方法只调用一次,所以heading总是设置为在开始时单击的项 public void onCreate(Bundle si) { Intent intent = getIntent(); heading = intent.getExtras().getString
Reports=>ReportsType
报表有3个项目,单击每个项目时,我将启动活动ReportsType
传递带有名称name
的标记,以区分单击了哪个项目
问题是OnCreate方法只调用一次,所以heading总是设置为在开始时单击的项
public void onCreate(Bundle si)
{
Intent intent = getIntent();
heading = intent.getExtras().getString("name"); //this tells which item was clicked.
TextView heading_txt = (TextView) findViewById(R.id.heading);
heading_txt.setText(heading);
}
我试图将此代码放在onResume()
调用中,因为每次活动恢复时都会调用它。但是,getIntent()
仍然给出了上一项中设置的旧的名称
值
如何在其他活动中获取当前单击的项目意图值
更新:
报告活动代码:
public void showReport(View v) {
String tag = v.getTag().toString();
Intent i5 = new Intent(this, ReportsType.class);
i5.putExtra("name", tag);
startActivity(i5);
}
其中,showReport()
方法在单击三个项中的任何一项时调用
更新:
goback报告代码
public void goBackReport(View v)
{
Intent intent = new Intent(ReportsType.this, Reports.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
finish();
}
XML按钮视图
<Button
android:id="@+id/entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/btn_blue_xml"
android:clickable="true"
android:padding="8dp"
android:onClick="goBackReport"
android:text="Back"
android:textColor="#ffffff"
android:textSize="15dp" />
在第二次
活动中尝试“喜欢”
if (getIntent().getExtras() != null) {
String mstr = getIntent().getExtras().getString("yourkey");
}
你的代码呢
Intent intent = getIntent();
heading = intent.getExtras().getString("name"); //this tells which item was clicked.
TextView heading_txt = (TextView) findViewById(R.id.heading);
heading_txt.setText(heading);
在不需要的地方放入
onItemclick
,然后在创建时放入onCreate
,最后通过在调用的活动中设置旧的intent值来解决,如下所示-
@Override
public void onNewIntent (Intent intent)
{
super.onNewIntent(intent);
setIntent(intent);
}
然后在onResume()调用-
@Override
public void onResume()
{
super.onResume();
Intent intent = getIntent();
heading = intent.getExtras().getString("name");
TextView heading_txt = (TextView) findViewById(R.id.heading);
heading_txt.setText(heading);
}
将您的报告活动代码张贴到我已理解您的问题,但我对问题感到困惑,因为创建方法只调用一次,所以标题总是设置为在开始时单击的项目
是否按后退按钮?确保将正确的视图传递给ShowReport()每个人的标签都不同views@Pragnani正确,我正在按下ReportsType
活动中的后退按钮,该按钮再次将我发送到报告
活动。然后单击下一项,但从标记中获得的值是第一项的值。因为OnCreate()
方法仅为第一项调用或仅调用一次。当您在ReportsType
中单击后退时,该活动将完成,您将返回到Reports
活动。如果您随后调用startActivity()
再次启动ReportsType
,则肯定会调用ReportsType.onCreate()
。这里还有别的事情。在ReportsType.onCreate()
中添加调试日志以查看它在Intent
中得到了什么,并在showReport()中添加调试日志以查看何时调用它以及发送什么。