Created
November 25, 2025 12:38
-
-
Save sunmeat/e1dea7c4beb46c9c45b607735da7395b to your computer and use it in GitHub Desktop.
from first activity to second and back android example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| MainActivity.java: | |
| package site.sunmeat.helloworld; | |
| import android.app.Activity; | |
| import android.content.Intent; | |
| import android.os.Bundle; | |
| import android.util.Log; | |
| import android.widget.*; | |
| import androidx.activity.result.ActivityResultLauncher; | |
| import androidx.activity.result.contract.ActivityResultContracts; | |
| import androidx.appcompat.app.AppCompatActivity; | |
| public class MainActivity extends AppCompatActivity { | |
| String TAG = "first_activity"; | |
| int value = 10; | |
| // це треба зробити полем, інакше виліт | |
| ActivityResultLauncher<Intent> l = registerForActivityResult( | |
| new ActivityResultContracts.StartActivityForResult(), | |
| result -> { | |
| if (result.getResultCode() == Activity.RESULT_OK) { | |
| Intent r = result.getData(); | |
| if (r != null) { | |
| int data = r.getIntExtra("result", 0); | |
| Toast.makeText(this, data + "", Toast.LENGTH_SHORT).show(); | |
| } | |
| } | |
| }); | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| Log.i(TAG, "FIRST ACTIVITY: onCreate"); | |
| Button b = findViewById(R.id.but1); | |
| b.setOnClickListener(v -> { | |
| var i = new Intent(this, SecondActivity.class); | |
| i.putExtra("data", value); | |
| Log.i(TAG, this + ""); | |
| l.launch(i); | |
| }); | |
| } | |
| @Override | |
| protected void onDestroy() { | |
| super.onDestroy(); | |
| Log.i(TAG, "FIRST ACTIVITY: onDestroy"); | |
| } | |
| @Override | |
| protected void onRestart() { | |
| super.onRestart(); | |
| Log.i(TAG, "FIRST ACTIVITY: onRestart"); | |
| } | |
| @Override | |
| protected void onStart() { | |
| super.onStart(); | |
| Log.i(TAG, "FIRST ACTIVITY: onStart"); | |
| } | |
| @Override | |
| protected void onStop() { | |
| super.onStop(); | |
| Log.i(TAG, "FIRST ACTIVITY: onStop"); | |
| } | |
| @Override | |
| protected void onResume() { | |
| super.onResume(); | |
| Log.i(TAG, "FIRST ACTIVITY: onResume"); | |
| } | |
| @Override | |
| protected void onPause() { | |
| super.onPause(); | |
| Log.i(TAG, "FIRST ACTIVITY: onPause"); | |
| } | |
| } | |
| ============================================================================================================= | |
| activity_main.xml: | |
| <?xml version="1.0" encoding="utf-8"?> | |
| <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| android:id="@+id/activity_main" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" | |
| android:background="#121926"> | |
| <Button | |
| android:id="@+id/but1" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" | |
| android:padding="4dp" | |
| android:text="FIRST ACTIVITY" /> | |
| </RelativeLayout> | |
| ============================================================================================================= | |
| SecondActivity.java: | |
| package site.sunmeat.helloworld; | |
| import android.app.Activity; | |
| import android.content.Intent; | |
| import android.os.Bundle; | |
| import android.util.Log; | |
| import android.widget.*; | |
| import androidx.appcompat.app.AppCompatActivity; | |
| public class SecondActivity extends AppCompatActivity { | |
| String TAG = "second_activity"; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_second); | |
| Log.i(TAG, this + ""); | |
| int value = 0; | |
| Intent parent = getIntent(); // отримуємо батьківський намір на створення актівіті (якщо він звісно існує) | |
| if (parent != null) { | |
| value = parent.getIntExtra("data", 0); | |
| Toast.makeText(this, value + "", Toast.LENGTH_SHORT).show(); | |
| } | |
| Log.i(TAG, "SECOND ACTIVITY: onCreate"); | |
| Button b = findViewById(R.id.but2); | |
| b.setOnClickListener(v -> { | |
| var intent = new Intent(); | |
| intent.putExtra("result", 221); | |
| setResult(Activity.RESULT_OK, intent); | |
| finish(); | |
| }); | |
| } | |
| @Override | |
| protected void onDestroy() { | |
| super.onDestroy(); | |
| Log.i(TAG, "SECOND ACTIVITY: onDestroy"); | |
| } | |
| @Override | |
| protected void onRestart() { | |
| super.onRestart(); | |
| Log.i(TAG, "SECOND ACTIVITY: onRestart"); | |
| } | |
| @Override | |
| protected void onStart() { | |
| super.onStart(); | |
| Log.i(TAG, "SECOND ACTIVITY: onStart"); | |
| } | |
| @Override | |
| protected void onStop() { | |
| super.onStop(); | |
| Log.i(TAG, "SECOND ACTIVITY: onStop"); | |
| } | |
| @Override | |
| protected void onResume() { | |
| super.onResume(); | |
| Log.i(TAG, "SECOND ACTIVITY: onResume"); | |
| } | |
| @Override | |
| protected void onPause() { | |
| super.onPause(); | |
| Log.i(TAG, "SECOND ACTIVITY: onPause"); | |
| } | |
| } | |
| ============================================================================================================= | |
| activity_second.xml: | |
| <?xml version="1.0" encoding="utf-8"?> | |
| <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| android:id="@+id/activity_main" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" | |
| android:background="#121926"> | |
| <Button | |
| android:id="@+id/but2" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" | |
| android:padding="4dp" | |
| android:text="SECOND ACTIVITY" /> | |
| </RelativeLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment