새로운 activity를 추가

1. Manifest file에 ativity tag가 자동으로 추가된다

2. 새로운 activity에 layout을 정의 

3. MainActivity에 새 Activity 띄우기(startActivity(), startActivityForResult())

4. 새 Activity에서 처리결과를 보내기(setResult())

5. MainActivity에서 onActivityResult() 를이용해 응답처리

//new\activity\empty activity

activity_main.xml

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="136dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="New activity"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

activity_main2.xml

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="260dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="new activity"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

AndroidManifest.xml

<activity android:name=".Main2Activity"
android:label = "new activity"
android:theme = "@style/Theme.AppCompat.Dialog.Alert"></activity>

MainActivity.java

public class MainActivity extends AppCompatActivity {

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

Button button = findViewById(R.id.btn1);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent(getApplicationContext(), Main2Activity.class);
startActivity(intent);
}
});
}
}

'android studio' 카테고리의 다른 글

intent  (0) 2019.08.14
startActivityForResult()(화면전환)  (0) 2019.08.13
Recycler View  (0) 2019.08.13
custom dialog  (0) 2019.08.13
alert dialog  (0) 2019.08.13

+ Recent posts