Intent and putExtra
Switching between activities in Android and sending data.
Sample pictures
Java code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | package com.example.IntentPutextra; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; Button button; EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.i(TAG, "m1_onCreate: "); button = findViewById(R.id.button); editText = findViewById(R.id.editText); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String str = editText.getText().toString(); Intent intent = new Intent(MainActivity.this,Main2Activity.class); intent.putExtra("ET",str); startActivity(intent); } }); } @Override protected void onResume() { super.onResume(); Log.i(TAG, "m1_onResume: "); } @Override protected void onStop() { super.onStop(); Log.i(TAG, "m1_onStop: "); } @Override protected void onPause() { super.onPause(); Log.i(TAG, "m1_onPause: "); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | package com.example.IntentPutextra; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.TextView; public class Main2Activity extends AppCompatActivity { private static final String TAG = "MainActivity"; TextView textView; MainActivity mm = new MainActivity(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); Log.i(TAG, "m2_onCreate: "); textView = findViewById(R.id.textView); Bundle bundle = getIntent().getExtras(); if (bundle != null) { String s = bundle.getString("ET"); textView.setText(s); } } @Override protected void onResume() { super.onResume(); Log.i(TAG, "m2_onResume: "); } @Override protected void onStop() { super.onStop(); Log.i(TAG, "m2_onStop: "); } @Override protected void onPause() { super.onPause(); Log.i(TAG, "m2_onPause: "); } } |