Android Activity與Fragment之間的跳轉(zhuǎn)實例詳解
更新時間:2017年02月03日 16:47:28 投稿:lqh
這篇文章主要介紹了Android Activity與Fragment之間的跳轉(zhuǎn)實例詳解的相關(guān)資料,需要的朋友可以參考下
Activity及Fragment之間的跳轉(zhuǎn)
直接跳轉(zhuǎn)
基本使用方法
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void jump(View view) {
Intent intent = new Intent(this, VideoPlayActivity.class);
intent.putExtra("video_id", "1");
startActivity(intent);
}
}
public class VideoPlayActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_play);
Intent intent = getIntent();
}
}
借助工具類
然而并沒什么卵用
public static void startActivity(Context context, Class clazz,Bundle data) {
Intent intent = new Intent(context, clazz);
if(data != null){
intent.putExtras(data);
}
context.startActivity(intent);
}
推薦方案
- 把new Intent()放在目標(biāo)Activity,這樣可以onCreate里的getIntent形成對應(yīng)
- 更容易理解和管理一個出口,多個入口的情況
- 較方便維護(hù)傳遞數(shù)據(jù)的key和value(即key由目標(biāo)Activity來定義,value是實際的來源)
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void jump(View view) {
startActivity(VideoPlayActivity.newIntent(this, "1"));
}
}
public class VideoPlayActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_play);
}
public static Intent newIntent(Context context, String vid) {
Intent intent = new Intent(context, VideoPlayActivity.class);
intent.putExtra("video_id", vid);
return intent;
}
}
Fragment跳轉(zhuǎn)
- 首先需要一個空的構(gòu)造方法,F(xiàn)ragment恢復(fù)重建要使用。
- 使用newInstance代替構(gòu)造方法傳參
- 在Fragment.onCreate方法獲取實參
public class MainFragment{
public MainFragment() {
// Required empty public constructor
}
public static MainFragment newInstance(int pos) {
MainFragment fragment = new MainFragment();
Bundle args = new Bundle();
args.putInt("pos", pos);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
pos = getArguments().getInt("pos");
}
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
探討Android 的屏幕滾動操作不如 iPhone 流暢順滑的原因
雖然很多Android手機(jī)的配置都比iPhone要高,比如大多數(shù)Andorid手機(jī)的內(nèi)存都有1GB,而iPhone 4S只有512MB內(nèi)存,但用過iPhone的人都知道Android手機(jī)在使用的時候總感覺沒有那么順滑,究竟為什么會出現(xiàn)這種現(xiàn)象呢?2014-07-07
Android中button實現(xiàn)onclicklistener事件的兩種方式
本文介紹下Android中button實現(xiàn)onclicklistener事件的兩種方法,感興趣的朋友可以參考下2013-04-04
Android Messenger實現(xiàn)進(jìn)程間通信及其原理
這篇文章主要為大家詳細(xì)介紹了Android Messenger實現(xiàn)進(jìn)程間通信及其原理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-05-05
Android游戲開發(fā)學(xué)習(xí)①彈跳小球?qū)崿F(xiàn)方法
這篇文章主要介紹了Android游戲開發(fā)學(xué)習(xí)①彈跳小球?qū)崿F(xiàn)方法,涉及Android通過物理引擎BallThread類模擬小球運動的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10
Android開發(fā)自學(xué)筆記(四):APP布局下
這篇文章主要介紹了Android開發(fā)自學(xué)筆記(四):APP布局下,本文是上一篇的補充,需要的朋友可以參考下2015-04-04
Android底部導(dǎo)航欄的三種風(fēng)格實現(xiàn)
這篇文章主要介紹了Android底部導(dǎo)航欄的三種風(fēng)格實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06

