![](/img/trans.png)
[英]How to use 'bundle' instead of 'intent.putExtra()' in Fragment in Android
[英]How do I use Intent.putExtra dynamically?
我有 2 個活動( MainActivity
, LoadIntentActivity
)。 我知道如何從MainActivity
中putExtra()
並使用getIntent().getExtra()
在LoadIntentActivity
中接收值。 但是,我想用 2 種方法啟動LoadIntentActivity
:一種是使用putExtra()
,另一種是不putExtra()
。 但是LoadIntentActivity
已經有一個getIntent().getExtra()
,如果我試圖在沒有putExtra()
的情況下啟動LoadIntentActivity
,它將導致NullPointerException
。
這是我的代碼:
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**
:
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**
:
<?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
:
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);
}
}
如果您想處理丟失包的情況,只需在使用之前檢查它(以及從中提取的字符串)是否為 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);
}
}
}
}
或者,如果您需要將文本保存為 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);
}
}
您還可以保存一個類似的 class 成員 boolean 以指示該活動是否已通過捆綁包啟動(以防它影響活動稍后采取的操作)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.