読者です 読者をやめる 読者になる 読者になる

liguofeng29’s blog

個人勉強用ブログだっす。

AndroidのActivity - Activityの起動と終了

MainActivity.java
 
package com.example.liguofeng.startandfinishactivity;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Intent生成
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
// SecondActivity表示
startActivity(intent);
}
});
}
}

 
SecondActivity.java
 
package com.example.liguofeng.startandfinishactivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);

Button back = (Button) findViewById(R.id.back);
final Button finish = (Button) findViewById(R.id.finish);

back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(SecondActivity.this, MainActivity.class);
startActivity(intent); // MainActiviy表示
}
});

finish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish(); // 終了
}
});
}
}