1、 Recovery and reconstruction: the elixir of data state
Scheme 1: Temporary storage of key data with Bundle
public class MyActivity extends AppCompatActivity { private static final String KEY_DATA = "key_data"; private String data; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState != null) { data = savedInstanceState.getString(KEY_DATA); } } @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); outState.putString(KEY_DATA, data); } }
Solution 2: ViewModel solves the ultimate data hosting
public class MyViewModel extends ViewModel { private Data currentData; public Data getCurrentData() { return currentData; } public void setData(Data data) { currentData = data; } }
public class MyActivity extends AppCompatActivity { private MyViewModel viewModel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewModel = new ViewModelProvider(this).get(MyViewModel.class); if (viewModel.getCurrentData() != null) { renderData(viewModel.getCurrentData()); } } private void renderData(Data data) { viewModel.setData(data); //TODO: update UI } }
2、 All knowing: Intent's multiple accurate control
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String data = "Hello"; Intent intent = new Intent(this, TargetActivity.class); intent.putExtra("KEY_DATA", data); startActivity(intent); } }
public class TargetActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_target); String data = getIntent().getStringExtra("KEY_DATA"); //TODO: process data } }
<!-- AndroidManifest.xml --> <activity android:name=".ShareActivity"> <intent-filter> <action android:name="android.intent.action.SEND" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="text/plain" /> </intent-filter> </activity>
public class SenderActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sender); Intent intent = new Intent(); intent.setAction(Intent. ACTION_SEND); intent.putExtra(Intent. EXTRA_TEXT, "Hello"); intent.setType("text/plain"); StartActivity (Intent. createChooser (intent, "shared text")); } }
3、 StartActivityForResult: Start and get the return
import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private TextView scoreTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); scoreTextView = findViewById(R.id.score_text_view); Button getScoreButton = findViewById(R.id.get_score_button); getScoreButton.setOnClickListener(new View. OnClickListener() { @Override public void onClick(View v) { //Start SecondActivity with startActivityForResult Intent intent = new Intent(MainActivity.this, SecondActivity.class); startActivityForResult(intent, 1); // 1 is the request code } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { //Check whether the request code matches if (requestCode == 1) { //Inspection result code if (resultCode == RESULT_OK) { //Get the returned data int score = data.getIntExtra("score", 0); scoreTextView.setText("Your score is: " + score); } else { //The result code is not RESULT_OK, indicating that the user canceled the operation scoreTextView.setText("No score was entered."); } } super.onActivityResult(requestCode, resultCode, data); } }
import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.content.Intent; public class SecondActivity extends AppCompatActivity { private EditText scoreEditText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); scoreEditText = findViewById(R.id.score_edit_text); Button submitButton = findViewById(R.id.submit_button); submitButton.setOnClickListener(new View. OnClickListener() { @Override public void onClick(View v) { //Get the score entered by the user int score = Integer.parseInt(scoreEditText.getText().toString()); //Create a new Intent to pass the results Intent resultIntent = new Intent(); resultIntent.putExtra("score", score); //End SecondActivity and pass the results setResult(RESULT_OK, resultIntent); finish(); } }); } }
MainActivity There is a button. When the user clicks this button, the startActivityForResult start-up SecondActivity 。 one It is the request code defined by us to distinguish onActivityResult Different requests in. SecondActivity An edit box is provided for users to enter scores, and there is a submit button. When the user clicks the submit button, the entered score will be obtained and put into a Intent Medium, and via setResult Method sends the result back to the Activity , and then end yourself. -
When SecondActivity At the end, MainActivity Of onActivityResult The method will be called. By checking the request code( requestCode )And result code( resultCode ), MainActivity You can determine whether the results are valid, and update the UI to display the scores entered by the user.
4、 Boundless: URI routing and Scheme protocol access
<activity android:name=".uri.UriActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:host="www.myapp.com" android:pathPrefix="/uri" android:scheme="https" /> </intent-filter> </activity>
public class UriActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_uri); Intent intent = getIntent(); Uri uri = intent.getData(); if (uri != null) { String path = uri.getPath(); // Get Path String param = uri.getQueryParameter("key"); // Get Parameters //TODO: Process the acquired data } } }
<!-- AndroidManifest.xml of other applications --> <activity android:name=". SchemeActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:host="open" android:scheme="myapp" /> </intent-filter> </activity>
//Request from our app String uri = " myapp://open?data=hello "; Intent intent = new Intent(Intent. ACTION_VIEW, Uri.parse(uri)); startActivity(intent);
The prologue is not over: more exploration is still going on