Common Events for Windows in Android Development Series III

  • 2021-07-09 09:19:04
  • OfStack

Related reading:

Window of Android Development Series 2 Activity Life Cycle

Android Development Series 1 Realizes Display Time with Buttons

Set the window header event and jump between Activity.

Create a new project, create two new Activity: MainActivity, TitleActivity, and then register TitleActivity in AnroidManifest. xml

AndroidManifest. xml reads as follows


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.neil.ad02">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".TitleActivity"
android:label="@string/app_name">
</activity>
</application>
</manifest>

In the onCreate method in MainActivity


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("onCreate","onCreate Method is executed");
Button bt=(Button)findViewById(R.id.btTurnTitle);
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,TitleActivity.class));
}
});
}

Clicking Button triggers onCreate () of TitleActivity- > onStart()- > onResume () method

Add an Button to activity_title. xml and click Button to change the value of app_name

Code in TitleActivity


package com.neil.ad;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
/**
* Created by Neil on //.
*/
public class TitleActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_title);
Button bt=(Button)findViewById(R.id.btTitle);
bt.setOnClickListener((View.OnClickListener) this);
setTitle(" Window title ");
}
public void onClick_SetTitle(View view)
{
setTitle(" New window title ");
setTitleColor();
}
// This method is called after the window has completely started 
@Override
protected void onPostCreate(Bundle savedInstanceState) {
Log.d("TitleActivity","onPostCreate");
super.onPostCreate(savedInstanceState);
}
@Override
protected void onTitleChanged(CharSequence title,int color)
{
super.onTitleChanged(title,color);
Log.d("TitleActivity","onTitleChanged_title"+title);
Log.d("TitleActivity","onTitleChanged_color"+color);
}
}

It's relatively simple, so I won't say much. If you have any questions, please leave me a message, and this site will reply to you in time!


Related articles: