[Android studio java learning notes] activity

Time:2020-6-9

1. Creation and completion of general activities

//Activity creation
Intent intent = new Intent(MainActivity.this,A01Activity.class);
startActivity(intent);

//End activity
finish();

When an activity activity01 is created, the following content is added automatically:

  • Activity01 is created in Java class
  • Activity added to layout file_ 01.xml
  • Register the activity < activity in the manifest file android:name= “.Activity1”></activity>

 

2. Start app

2.1 telephone

String strTel = mainEditText.getText().toString();

Uri uri = Uri.parse("tel:"+strTel);
Intent intent = new Intent(Intent.ACTION_DIAL,uri);
startActivity(intent);

2.2 SMS

String strTel =  mainEditText.getText().toString();
String strSms = messageEditText.getText().toString();

Uri uri = Uri.parse("smsto:"+ strTel);
Intent intent = new Intent(Intent.ACTION_SENDTO,uri);
intent.putExtra("sms_body", strSms);
startActivity(intent);

2.3 web page

String strUrl = webEditText.getText().toString();

Uri uri = Uri.parse(strUrl);
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);

 

3. Data transfer between activities

3.1 send to new activity one by one

//Data sending activity
String strTel = mainEditText.getText().toString();
Intent intent = new Intent(MainActivity.this,Activity1.class);
intent.putExtra("Tel",strTel);
startActivity(intent);

//Data receiving activity
Intent intent = getIntent();
String strTel = intent.getStringExtra("Tel");

3.2 package and send to new activity

//Data sending activity
Intent intent = new Intent(MainActivity.this,Activity1.class);
Bundle bundle = new Bundle();
bundle.putString("Father","180");
bundle.putString("Mather","165");
intent.putExtras(bundle);
startActivity(intent);

//Data receiving activity
Intent intent = getIntent();
Bundle bundle = intent.getExtras();

fatherHeightText.setText(bundle.getString("Father"));
motherHeightText.setText(bundle.getString("Mather"));

3.3 data return

//Activity 1 - send data
Intent intent = new Intent(MainActivity.this,DataInputActivate.class);

Bundle bundle = new Bundle();
bundle.putString("Father","180");
bundle.putString("Mother","165");
intent.putExtras(bundle);           

startActivityForResult(intent,1);

//Activity 2 -- get the initial data sent by the previous activity
Intent intent = getIntent();
Bundle bundle = intent.getExtras();

etFather.setText(bundle.getString("Father"));
etMother.setText(bundle.getString("Mother"));

//Activity 2 -- return
Intent intent1 = new Intent();
intent1.putExtra("Boy",intBoy);
intent1.putExtra("Girl",intGirl);
setResult(10,intent1);
finish();

//Activity 1 -- get result event
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
  super.onActivityResult(requestCode, resultCode, data);

  if(requestCode==1)
  {
     if(resultCode==10){
        tvMsg.setText (boy:+ String.valueOf ( data.getIntExtra ("boy", 0)) + "girl:"+ String.valueOf ( data.getIntExtra ("Girl",0))  );
       }
   }
}

 

4. Life cycle of activity

The following is a brief description. For a detailed comparison of events, please refer to the in-depth understanding of activity’s life cycle

meaning

The whole process of an activity from creation to destruction.

Life cycle related events and sequence of execution

(1)onCreate
Called when the activity is created for the first time, in this method, basic application startup logic is executed, such as declaring interface, defining member variables, and configuring some interfaces.
Called when an activity is created in the background.

(2)onStart
This callback is called when the activity becomes visible, but it is not in the foreground at this time and cannot interact.
Called when the activity is from background to foreground.

(3)onResume
When the activity is at the top of the stack, in the running state, and can interact with the user, the onresume method is called.
Activity is called when the foreground can interact.

(4)onPause
When the activity has lost focus but is still visible, the onpause method is called. At this time, the activity cannot interact with the user. Generally, the onstop method is called back immediately.
Called when the activity is not interactive in the foreground.

(5)onStop
When the activity changes from visible to invisible, the onstop method is called.
Activity is called from the foreground to the background.

(6)onRestart
Called when an activity is restarted, not when it is first created.
The activity is called again from the background to the foreground.

(7)onDestroy
Before Activity is destroyed, it is called.
Called when the activity is destroyed in the background.