Android從Fragment跳轉(zhuǎn)到其他Activity的簡單實例
為了更好的理解以下內(nèi)容,我們需要簡單了解一下Fragment的動態(tài)注冊方法
Android——Fragment的靜態(tài)注冊和動態(tài)注冊
為了實現(xiàn)從Fragment跳轉(zhuǎn)到其他Activity,下面需要創(chuàng)建以下文件:

第一步:簡單編寫布局文件fragment_activity.xml和抽象類TemplateFragmentActivity.java代碼如下:
fragment_activity.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/temp_fragment_activity" android:layout_width="match_parent" android:layout_height="match_parent"> </FrameLayout>
fragment_activity.xml布局主要用于承載各fragment布局,例如fragment_one.xml和fragment_two.xml。
TemplateFragmentActivity.java
package com.example.myapplication;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
public abstract class TemplateFragmentActivity extends AppCompatActivity
{
private FragmentManager fm;
private FragmentTransaction ts;
private Fragment fragment;
//抽象方法,用于創(chuàng)建Fragment實例
protected abstract Fragment createFragment();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_activity);
fm = getSupportFragmentManager();
ts = fm.beginTransaction();
if (fragment == null){
fragment = createFragment();
ts.add(R.id.temp_fragment_activity,fragment);
ts.commit();
}
}
}
第二步:分別使類FragmentOneActivity和FragmentTwoActivity繼承類TemplateFragmentActivity并實現(xiàn)抽象方法createFragment()
FragmentOneActivity.java
package com.example.myapplication;
import androidx.fragment.app.Fragment;
public class FragmentOneActivity extends TemplateFragmentActivity {
@Override
protected Fragment createFragment() {
return new FragmentOne();
}
}
FragmentTwoActivity.java與FragmentOneActivity.java類似,不在重復(fù)。
第三步:分別編寫fragment_one.xml和fragment_two.xml布局文件并通過編寫FragmentOne.java和FragmentTwo.java綁定對應(yīng)的布局文件,并實現(xiàn)其具體功能。
fragment_one.xml
<?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" android:gravity="center" android:background="@color/colorPrimaryDark" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="點擊下面的按鈕跳轉(zhuǎn)到FragmentTwoActivity" android:textSize="20sp" android:textAllCaps="false" android:textColor="#F70505"> </TextView> <Button android:id="@+id/btn_fm_one" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="跳轉(zhuǎn)" android:textSize="30dp" android:layout_marginTop="20dp"> </Button> </LinearLayout>
fragment_two.xml與fragment_one.xml類似,不在重復(fù)。
FragmentOne.java
package com.example.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class FragmentOne extends Fragment {
private Button mBtnFragmentOne;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one,container,false);
mBtnFragmentOne = view.findViewById(R.id.btn_fm_one);
mBtnFragmentOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(),FragmentTwoActivity.class);
startActivity(intent);
}
});
return view;
}
}
Fragment跳轉(zhuǎn)到Activity與Activity跳轉(zhuǎn)到Activity方法類似,如下:
Intent intent = new Intent(getActivity(),FragmentTwoActivity.class); startActivity(intent);
FragmentTwo.java與FragmentOne .java類似,不在重復(fù)。
演示·:

總結(jié)
以上所述是小編給大家介紹的Android從Fragment跳轉(zhuǎn)到其他Activity的簡單實例,希望對大家有所幫助!
相關(guān)文章
android效果TapBarMenu繪制底部導(dǎo)航欄的使用方式示例
本篇文章主要介紹了android效果TapBarMenu繪制底部導(dǎo)航欄的使用方式,具有一定的參考價值,有興趣的可以了解一下。2017-01-01
Android使用fastjson庫解析json字符串實戰(zhàn)
fastjson是一個Java語言編寫的高性能功能完善的JSON庫,它采用一種“假定有序快速匹配”的算法,把JSON?Parse的性能提升到極致,是目前Java語言中最快的JSON庫,Fastjson接口簡單易用,已經(jīng)被廣泛使用在緩存序列化、協(xié)議交互、Web輸出、Android客戶端等多種應(yīng)用場景2023-11-11
Android框架Volley使用:ImageRequest請求實現(xiàn)圖片加載
這篇文章主要介紹了Android框架Volley使用:ImageRequest請求實現(xiàn)圖片加載的相關(guān)知識,非常不錯,具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-05-05
Android 自定義View實現(xiàn)任意布局的RadioGroup效果
這篇文章主要介紹了Android 自定義View實現(xiàn)任意布局的RadioGroup,需要的朋友可以參考下2018-11-11
Android Camera2實現(xiàn)最簡單的預(yù)覽框顯示
這篇文章主要為大家詳細(xì)介紹了Android Camera2實現(xiàn)最簡單的預(yù)覽框顯示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05

