一、使用 startActivity 跳转
<?xml version="1.0" encoding="utf-8"?> < androidx.constraintlayout.widget.ConstraintLayout xmlns: android= " http://schemas.android.com/apk/res/android" xmlns: app= " http://schemas.android.com/apk/res-auto" xmlns: tools= " http://schemas.android.com/tools" android: layout_width= " match_parent" android: layout_height= " match_parent" tools: context= " .fragment.TestFragment" > < TextViewandroid: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " TestFragment" app: layout_constraintEnd_toEndOf= " parent" app: layout_constraintStart_toStartOf= " parent" app: layout_constraintTop_toTopOf= " parent" /> < Buttonandroid: id= " @+id/btn_jump" android: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " 跳转" app: layout_constraintBottom_toBottomOf= " parent" app: layout_constraintEnd_toEndOf= " parent" app: layout_constraintStart_toStartOf= " parent" />
</ androidx.constraintlayout.widget.ConstraintLayout>
2、Activity Code
public class TestFragment
extends Fragment {
public static final String TAG = TestFragment . class .
getSimpleName ( ) ;
@Override
public View onCreateView ( LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater. inflate ( R . layout. fragment_test, container, false ) ;
}
@Override
public void onViewCreated ( @NonNull View view, @Nullable Bundle savedInstanceState) {
super . onViewCreated ( view, savedInstanceState) ;
Button btnJump = view. findViewById ( R . id. btn_jump) ;
btnJump. setOnClickListener ( v ->
{
Intent intent = new Intent ( getActivity ( ) , Test2Activity . class )
;
startActivity ( intent) ;
} ) ;
}
}
3、Activity Layout
<?xml version="1.0" encoding="utf-8"?> < androidx.constraintlayout.widget.ConstraintLayout xmlns: android= " http://schemas.android.com/apk/res/android" xmlns: app= " http://schemas.android.com/apk/res-auto" xmlns: tools= " http://schemas.android.com/tools" android: id= " @+id/main" android: layout_width= " match_parent" android: layout_height= " match_parent" tools: context= " .Test1Activity" > < TextViewandroid: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " Test1Activity" app: layout_constraintEnd_toEndOf= " parent" app: layout_constraintStart_toStartOf= " parent" app: layout_constraintTop_toTopOf= " parent" /> < fragmentandroid: name= " com.my.navigation.fragment.TestFragment" android: layout_width= " match_parent" android: layout_height= " 300dp" app: layout_constraintBottom_toBottomOf= " parent" app: layout_constraintEnd_toEndOf= " parent" app: layout_constraintStart_toStartOf= " parent" />
</ androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?> < androidx.constraintlayout.widget.ConstraintLayout xmlns: android= " http://schemas.android.com/apk/res/android" xmlns: app= " http://schemas.android.com/apk/res-auto" xmlns: tools= " http://schemas.android.com/tools" android: id= " @+id/main" android: layout_width= " match_parent" android: layout_height= " match_parent" tools: context= " .Test1Activity" > < TextViewandroid: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " Test2Activity" app: layout_constraintEnd_toEndOf= " parent" app: layout_constraintStart_toStartOf= " parent" app: layout_constraintTop_toTopOf= " parent" /> < Buttonandroid: id= " @+id/btn_back" android: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " 返回" app: layout_constraintBottom_toBottomOf= " parent" app: layout_constraintEnd_toEndOf= " parent" app: layout_constraintStart_toStartOf= " parent" />
</ androidx.constraintlayout.widget.ConstraintLayout>
4、Activity Code
public class Test1Activity
extends AppCompatActivity {
@Override
protected void onCreate ( Bundle savedInstanceState) {
super . onCreate ( savedInstanceState) ;
EdgeToEdge . enable ( this ) ;
setContentView ( R . layout. activity_test1) ;
ViewCompat . setOnApplyWindowInsetsListener ( findViewById ( R . id. main) , ( v, insets) ->
{
Insets systemBars = insets. getInsets ( WindowInsetsCompat. Type . systemBars ( ) ) ;
v. setPadding ( systemBars. left, systemBars. top, systemBars. right, systemBars. bottom) ;
return insets;
} ) ;
}
}
public class Test2Activity
extends AppCompatActivity {
@Override
protected void onCreate ( Bundle savedInstanceState) {
super . onCreate ( savedInstanceState) ;
EdgeToEdge . enable ( this ) ;
setContentView ( R . layout. activity_test2) ;
ViewCompat . setOnApplyWindowInsetsListener ( findViewById ( R . id. main) , ( v, insets) ->
{
Insets systemBars = insets. getInsets ( WindowInsetsCompat. Type . systemBars ( ) ) ;
v. setPadding ( systemBars. left, systemBars. top, systemBars. right, systemBars. bottom) ;
return insets;
} ) ;
Button btnBack = findViewById ( R . id. btn_back) ;
btnBack. setOnClickListener ( v ->
{
finish ( ) ;
} ) ;
}
}
二、使用 Activity Result API 跳转
ActivityResultLauncher <
Intent > activityResultLauncher = registerForActivityResult (
new ActivityResultContracts. StartActivityForResult ( ) ,
result ->
{
if ( result. getResultCode ( ) == Activity . RESULT_OK) {
Intent data = result. getData ( ) ;
int num = data. getIntExtra ( "num" , 0 ) ;
Log . i ( TAG, "num: " + num) ;
}
}
) ;
Button btnJump = view. findViewById ( R . id. btn_jump) ;
btnJump. setOnClickListener ( v ->
{
Intent intent = new Intent ( getActivity ( ) , Test2Activity . class )
;
activityResultLauncher. launch ( intent) ;
} ) ;
2、Activity Code
Button btnBack = findViewById ( R . id. btn_back) ;
btnBack. setOnClickListener ( v ->
{
Intent intent = new Intent ( ) ;
intent. putExtra ( "num" , 100 ) ;
setResult ( RESULT_OK, intent) ;
finish ( ) ;
} ) ;
三、使用 startActivityForResult 方法跳转
private static final int REQUEST_CODE = 100 ;
@Override
public void onActivityResult ( int requestCode, int resultCode, @Nullable Intent data) {
super . onActivityResult ( requestCode, resultCode, data) ;
if ( requestCode == REQUEST_CODE) {
if ( resultCode == Activity . RESULT_OK) {
int num = data. getIntExtra ( "num" , 0 ) ;
Log . i ( TAG, "num: " + num) ;
}
}
}
Button btnJump = view. findViewById ( R . id. btn_jump) ;
btnJump. setOnClickListener ( v ->
{
Intent intent = new Intent ( getActivity ( ) , Test2Activity . class )
;
startActivityForResult ( intent, REQUEST_CODE) ;
} ) ;
2、Activity Code
Button btnBack = findViewById ( R . id. btn_back) ;
btnBack. setOnClickListener ( v ->
{
Intent intent = new Intent ( ) ;
intent. putExtra ( "num" , 100 ) ;
setResult ( RESULT_OK, intent) ;
finish ( ) ;
} ) ;
四、使用 Navigation 跳转
<?xml version="1.0" encoding="utf-8"?> < androidx.constraintlayout.widget.ConstraintLayout xmlns: android= " http://schemas.android.com/apk/res/android" xmlns: app= " http://schemas.android.com/apk/res-auto" xmlns: tools= " http://schemas.android.com/tools" android: layout_width= " match_parent" android: layout_height= " match_parent" tools: context= " .fragment.TestFragment" > < TextViewandroid: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " TestFragment" app: layout_constraintEnd_toEndOf= " parent" app: layout_constraintStart_toStartOf= " parent" app: layout_constraintTop_toTopOf= " parent" /> < Buttonandroid: id= " @+id/btn_jump" android: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " 跳转" app: layout_constraintBottom_toBottomOf= " parent" app: layout_constraintEnd_toEndOf= " parent" app: layout_constraintStart_toStartOf= " parent" />
</ androidx.constraintlayout.widget.ConstraintLayout>
2、Fragment Code
public class TestFragment
extends Fragment {
public static final String TAG = TestFragment . class .
getSimpleName ( ) ;
@Override
public View onCreateView ( LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater. inflate ( R . layout. fragment_test, container, false ) ;
}
@Override
public void onViewCreated ( @NonNull View view, @Nullable Bundle savedInstanceState) {
super . onViewCreated ( view, savedInstanceState) ;
NavController navController = Navigation . findNavController ( view) ;
Button btnJump = view. findViewById ( R . id. btn_jump) ;
btnJump. setOnClickListener ( v ->
{
navController. navigate ( R . id. action_testFragment_to_test2Activity) ;
} ) ;
}
}
3、Navigation Graphtest_graph_navigation.xml
<?xml version="1.0" encoding="utf-8"?> < navigation xmlns: android= " http://schemas.android.com/apk/res/android" xmlns: app= " http://schemas.android.com/apk/res-auto" xmlns: tools= " http://schemas.android.com/tools" android: id= " @+id/test_graph_navigation" app: startDestination= " @id/testFragment" > < fragmentandroid: id= " @+id/testFragment" android: name= " com.my.navigation.fragment.TestFragment" android: label= " fragment_test" tools: layout= " @layout/fragment_test" > < actionandroid: id= " @+id/action_testFragment_to_test2Activity" app: destination= " @id/test2Activity" />
</ fragment>
< activityandroid: id= " @+id/test2Activity" android: name= " com.my.navigation.Test2Activity" android: label= " Test2Activity" />
</ navigation>
4、Activity Layout
<?xml version="1.0" encoding="utf-8"?> < androidx.constraintlayout.widget.ConstraintLayout xmlns: android= " http://schemas.android.com/apk/res/android" xmlns: app= " http://schemas.android.com/apk/res-auto" xmlns: tools= " http://schemas.android.com/tools" android: id= " @+id/main" android: layout_width= " match_parent" android: layout_height= " match_parent" tools: context= " .Test1Activity" > < TextViewandroid: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " Test1Activity" app: layout_constraintEnd_toEndOf= " parent" app: layout_constraintStart_toStartOf= " parent" app: layout_constraintTop_toTopOf= " parent" /> < fragmentandroid: id= " @+id/nhf" android: name= " androidx.navigation.fragment.NavHostFragment" android: layout_width= " match_parent" android: layout_height= " 300dp" app: layout_constraintBottom_toBottomOf= " parent" app: layout_constraintEnd_toEndOf= " parent" app: layout_constraintStart_toStartOf= " parent" app: navGraph= " @navigation/test_graph_navigation" />
</ androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?> < androidx.constraintlayout.widget.ConstraintLayout xmlns: android= " http://schemas.android.com/apk/res/android" xmlns: app= " http://schemas.android.com/apk/res-auto" xmlns: tools= " http://schemas.android.com/tools" android: id= " @+id/main" android: layout_width= " match_parent" android: layout_height= " match_parent" tools: context= " .Test1Activity" > < TextViewandroid: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " Test2Activity" app: layout_constraintEnd_toEndOf= " parent" app: layout_constraintStart_toStartOf= " parent" app: layout_constraintTop_toTopOf= " parent" /> < Buttonandroid: id= " @+id/btn_back" android: layout_width= " wrap_content" android: layout_height= " wrap_content" android: text= " 返回" app: layout_constraintBottom_toBottomOf= " parent" app: layout_constraintEnd_toEndOf= " parent" app: layout_constraintStart_toStartOf= " parent" />
</ androidx.constraintlayout.widget.ConstraintLayout>
5、Activity Code
public class Test1Activity
extends AppCompatActivity {
@Override
protected void onCreate ( Bundle savedInstanceState) {
super . onCreate ( savedInstanceState) ;
EdgeToEdge . enable ( this ) ;
setContentView ( R . layout. activity_test1) ;
ViewCompat . setOnApplyWindowInsetsListener ( findViewById ( R . id. main) , ( v, insets) ->
{
Insets systemBars = insets. getInsets ( WindowInsetsCompat. Type . systemBars ( ) ) ;
v. setPadding ( systemBars. left, systemBars. top, systemBars. right, systemBars. bottom) ;
return insets;
} ) ;
}
}
public class Test2Activity
extends AppCompatActivity {
@Override
protected void onCreate ( Bundle savedInstanceState) {
super . onCreate ( savedInstanceState) ;
EdgeToEdge . enable ( this ) ;
setContentView ( R . layout. activity_test2) ;
ViewCompat . setOnApplyWindowInsetsListener ( findViewById ( R . id. main) , ( v, insets) ->
{
Insets systemBars = insets. getInsets ( WindowInsetsCompat. Type . systemBars ( ) ) ;
v. setPadding ( systemBars. left, systemBars. top, systemBars. right, systemBars. bottom) ;
return insets;
} ) ;
Button btnBack = findViewById ( R . id. btn_back) ;
btnBack. setOnClickListener ( v ->
{
finish ( ) ;
} ) ;
}
}