繁体   English   中英

如何动态使用 Intent.putExtra?

[英]How do I use Intent.putExtra dynamically?

我有 2 个活动( MainActivityLoadIntentActivity )。 我知道如何从MainActivityputExtra()并使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM