[英]How do I use Intent.putExtra dynamically?
I have 2 Activities which are ( MainActivity
, LoadIntentActivity
).我有 2 个活动(
MainActivity
, LoadIntentActivity
)。 I know how to putExtra()
from MainActivity
and receive the value in LoadIntentActivity
with getIntent().getExtra()
.我知道如何从
MainActivity
中putExtra()
并使用getIntent().getExtra()
在LoadIntentActivity
中接收值。 However, I want to start LoadIntentActivity
with 2 method: 1 is with the putExtra()
and the other is without putExtra()
.但是,我想用 2 种方法启动
LoadIntentActivity
:一种是使用putExtra()
,另一种是不putExtra()
。 But the LoadIntentActivity
already have a getIntent().getExtra()
that will result NullPointerException
if i trying to start LoadIntentActivity
without putExtra()
.但是
LoadIntentActivity
已经有一个getIntent().getExtra()
,如果我试图在没有putExtra()
的情况下启动LoadIntentActivity
,它将导致NullPointerException
。
This is my code:这是我的代码:
activity_main.xml
: activity_main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btnGotoIA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Go to IntentActivity"/>
<ListView
android:id="@+id/lvList"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
**MainActivity.java**
: **MainActivity.java**
:
public class MainActivity extends AppCompatActivity {
Button btnGotoIA;
ListView lvList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnGotoIA = findViewById(R.id.btnGotoIA);
lvList = findViewById(R.id.lvList);
String[] items = {"1", "4", "53", "3", "99"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
lvList.setAdapter(adapter);
btnGotoIA.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, LoadIntentActivity.class);
startActivity(intent);
}
});
lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(MainActivity.this, LoadIntentActivity.class);
intent.putExtra("text", adapter.getItem(i));
startActivity(intent);
}
});
}
}
**activity_load_intent.xml**
: **activity_load_intent.xml**
:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoadIntentActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
LoadIntentActivity.java
: LoadIntentActivity.java
:
public class LoadIntentActivity extends AppCompatActivity {
TextView textView;
Bundle bundle = getIntent().getExtras();
String text = bundle.getString("text");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_load_intent);
textView = findViewById(R.id.textView);
// if i start this Activity without putExtra the TextView should be empty
textView.setText(text);
}
}
If you want to handle the case of a missing bundle, just check if it (and the string extracted from it) is null before using it.如果您想处理丢失包的情况,只需在使用之前检查它(以及从中提取的字符串)是否为 null。
public class LoadIntentActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_load_intent);
textView = findViewById(R.id.textView);
Bundle bundle = getIntent().getExtras();
if( bundle != null ) {
String bText = bundle.getString("text");
if( bText != null ) {
textView.setText(bText);
}
}
}
}
Alternately, if you need to save the text as a class member still you could do something like this:或者,如果您需要将文本保存为 class 成员,您仍然可以执行以下操作:
public class LoadIntentActivity extends AppCompatActivity {
private TextView textView;
private String text = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_load_intent);
textView = findViewById(R.id.textView);
Bundle bundle = getIntent().getExtras();
if( bundle != null ) {
String bText = bundle.getString("text");
if( bText != null ) {
text = bText;
}
}
textView.setText(text);
}
}
You could also save a similar class member boolean to indicate whether the activity had been started with a bundle or not (in case it affects the actions taken later in the activity).您还可以保存一个类似的 class 成员 boolean 以指示该活动是否已通过捆绑包启动(以防它影响活动稍后采取的操作)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.