我的代码流程是:
Reports => ReportsType
报告有3个项目,点击每个项目后,我开始活动ReportsType
,在意图中传递名称为name
的标记,以区分点击了哪个项目。
问题是OnCreate方法只被调用一次,因此始终将标题设置为在开始时单击的项目。
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()
仍然是从前一项中提供旧的name
值。
如何在其他活动中获取当前点击的项目意图值?
更新:
报告活动代码:
public void showReport(View v) {
String tag = v.getTag().toString();
Intent i5 = new Intent(this, ReportsType.class);
i5.putExtra("name", tag);
startActivity(i5);
}
如果为每个项目调用showReport()
方法,则单击这三个项目中的任何一个。
更新:
goBackReport代码
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" />
答案 0 :(得分:2)
最后通过在被调用的活动中设置旧的意图值来解决这个问题 -
@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);
}
答案 1 :(得分:0)
在第二个Activity
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
不需要的地方