<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="이게 세컨드입니다."
android:id="@+id/bt_second"></Button>
</LinearLayout>
package com.cookandroid.w11;
public class SecondActivity {
}
package com.cookandroid.w11;
import android.app.Activity;
public class SecondActivity extends Activity {
}
package com.cookandroid.w11;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt = (Button) findViewById(R.id.bt_main);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Intent 선언, MainActivity.this가 SecondActivity.class를 불러내게 된다.
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
//생성한 intent를 파라미터로 전달하면, startActivity가 전달받은 intent를 이용해서 SecondActivity를 실행시켜줌.
startActivity(intent);
}
});
}
}
package com.cookandroid.w11;
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 {
//MainActivity에서 onCreat복붙해옴. Code-OverrideMethods에서 가져와도 되지만.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//복붙해왔다면 R.layout뒤에 second로 수정해주기
setContentView(R.layout.second);
//MainActivity에서 가져와 수정하기
Button bt = (Button) findViewById(R.id.bt_second);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//intent 실행이 아니라 Second 액티비티가 종료되게. finish
finish();
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cookandroid.w11">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.W11">
<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=".SecondActivity"></activity>
</application>
</manifest>