亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android 管理Activity中的fragments

 更新時(shí)間:2013年01月10日 09:42:46   作者:  
為了管理Activity中的fragments,需要使用FragmentManager,為了得到它,需要調(diào)用Activity中的getFragmentManager()方法,接下來詳細(xì)介紹,感興趣的朋友可以了解下哦
FragmentManager
為了管理Activity中的fragments,需要使用FragmentManager.
為了得到它,需要調(diào)用Activity中的getFragmentManager()方法。
因?yàn)镕ragmentManager的API是在Android 3.0,也即API level 11開始引入的,所以對于之前的版本,需要使用support library中的FragmentActivity,并且使用getSupportFragmentManager()方法。
用FragmentManager可以做的工作有:
得到Activity中存在的fragment
使用findFragmentById()或findFragmentByTag()方法。
將fragment彈出back stack
popBackStack():將back stack中最后一次的fragment轉(zhuǎn)換彈出。如果沒有可以出棧的東西,返回false。
這個(gè)函數(shù)是異步的:它將彈出棧的請求加入隊(duì)列,但是這個(gè)動(dòng)作直到應(yīng)用回到事件循環(huán)才會(huì)執(zhí)行。
為back stack加上監(jiān)聽器
addOnBackStackChangedListener()
Performing Fragment Transactions
使用Fragment時(shí),可以通過用戶交互來執(zhí)行一些動(dòng)作,比如增加、移除、替換等。
所有這些改變構(gòu)成一個(gè)集合,這個(gè)集合被叫做一個(gè)transaction。
可以調(diào)用FragmentTransaction中的方法來處理這個(gè)transaction,并且可以將transaction存進(jìn)由activity管理的back stack中,這樣用戶就可以進(jìn)行fragment變化的回退操作。
可以這樣得到FragmentTransaction類的實(shí)例: 
復(fù)制代碼 代碼如下:

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

每個(gè)transaction是一組同時(shí)執(zhí)行的變化的集合。
用add(), remove(), replace()方法,把所有需要的變化加進(jìn)去,然后調(diào)用commit()方法,將這些變化應(yīng)用。
在commit()方法之前,你可以調(diào)用addToBackStack(),把這個(gè)transaction加入back stack中去,這個(gè)back stack是由activity管理的,當(dāng)用戶按返回鍵時(shí),就會(huì)回到上一個(gè)fragment的狀態(tài)。
比如下面的代碼就是用一個(gè)新的fragment取代之前的fragment,并且將前次的狀態(tài)存儲(chǔ)在back stack中。
復(fù)制代碼 代碼如下:

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();

在這個(gè)例子中,newFragment將取代在R.id.fragment_container容器中的fragment,如果沒有,將直接添加新的fragment。

通過調(diào)用addToBackStack(),commit()的一系列轉(zhuǎn)換作為一個(gè)transaction被存儲(chǔ)在back stack中,用戶按Back鍵可以返回上一個(gè)轉(zhuǎn)換前的狀態(tài)。

當(dāng)你移除一個(gè)fragment的時(shí)候,如果commit()之前沒有調(diào)用addToBackStack(),那個(gè)fragment將會(huì)是destroyed;如果調(diào)用了addToBackStack(),這個(gè)fragment會(huì)是stopped,可以通過返回鍵來恢復(fù)。
關(guān)于commit()方法
調(diào)用commit()方法并不能立即執(zhí)行transaction中包含的改變動(dòng)作,commit()方法把transaction加入activity的UI線程隊(duì)列中。

但是,如果覺得有必要的話,可以調(diào)用executePendingTransactions()方法來立即執(zhí)行commit()提供的transaction。
這樣做通常是沒有必要的,除非這個(gè)transaction被其他線程依賴。
注意:你只能在activity存儲(chǔ)它的狀態(tài)(當(dāng)用戶要離開activity時(shí))之前調(diào)用commit(),如果在存儲(chǔ)狀態(tài)之后調(diào)用commit(),將會(huì)拋出一個(gè)異常。

這是因?yàn)楫?dāng)activity再次被恢復(fù)時(shí)commit之后的狀態(tài)將丟失。如果丟失也沒關(guān)系,那么使用commitAllowingStateLoss()方法。
實(shí)例程序
寫了個(gè)小程序?qū)嵺`了一下fragment的管理,程序不是很完善,就是試試基本用法,先按第一個(gè)按鈕添加一個(gè)fragment,第二個(gè)按鈕將其替換,第三個(gè)按鈕將第二個(gè)按鈕添加的fragment刪除。
相關(guān)代碼:
第一個(gè)fragment
復(fù)制代碼 代碼如下:

ExampleFragment.java
package com.example.learningfragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ExampleFragment extends Fragment
{
//三個(gè)一般必須重載的方法
@Override
public void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
System.out.println("ExampleFragment--onCreate");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
System.out.println("ExampleFragment--onCreateView");
return inflater.inflate(R.layout.example_fragment_layout, container, false);
}
@Override
public void onPause()
{
// TODO Auto-generated method stub
super.onPause();
System.out.println("ExampleFragment--onPause");
}
@Override
public void onResume()
{
// TODO Auto-generated method stub
super.onResume();
System.out.println("ExampleFragment--onResume");
}
@Override
public void onStop()
{
// TODO Auto-generated method stub
super.onStop();
System.out.println("ExampleFragment--onStop");
}
}

它的布局
復(fù)制代碼 代碼如下:

example_fragment_layout.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:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:textSize="20dip"
android:text="@string/fragment1"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/num1"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/num2"
/>
</LinearLayout>

第二個(gè)fragment:
復(fù)制代碼 代碼如下:

NewFragment.java
package com.example.learningfragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class NewFragment extends Fragment
{
//三個(gè)一般必須重載的方法
@Override
public void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
System.out.println("NewFragment--onCreate");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
System.out.println("NewFragment--onCreateView");
return inflater.inflate(R.layout.new_fragment_layout, container, false);
}
@Override
public void onPause()
{
// TODO Auto-generated method stub
super.onPause();
System.out.println("NewFragment--onPause");
}
}

復(fù)制代碼 代碼如下:

new_fragment_layout.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:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dip"
android:gravity="left"
android:text="@string/fragment2"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/num3"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/num4"
/>
</LinearLayout>

主Activity
復(fù)制代碼 代碼如下:

LearnFragment.java
package com.example.learningfragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
public class LearnFragment extends FragmentActivity
{
Button btn1;
Button btn2;
Button btn3;
ExampleFragment fragmentE;
NewFragment fragmentN;
FragmentManager fragmentManager;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_learn_fragment);
findViews();
setListeners();
//獲得Fragment管理所需要的類的對象
fragmentManager = getSupportFragmentManager();
}
private void findViews()
{
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
}
private void setListeners()
{
//第一個(gè)按鈕,增加一個(gè)ExampleFragment
btn1.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
//在程序中加入ExampleFragment
fragmentE = new ExampleFragment();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.linear1,fragmentE);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
);
//第二個(gè)按鈕,用一個(gè)NewFragment替換前面增加的那個(gè)fragment
btn2.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
fragmentN = new NewFragment();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.linear1,fragmentN);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
);
//第三個(gè)按鈕,移除fragment
btn3.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(fragmentN);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
);
}
}

復(fù)制代碼 代碼如下:

activity_learn_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linear1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dip"
android:gravity="center_horizontal"
android:text="@string/layout1"
/>
<Button
android:id="@+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn1"
/>
<fragment
android:name="com.example.learningfragment.ExampleFragment"
android:id="@+id/fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn2"
/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dip"
android:gravity="center_horizontal"
android:text="@string/layout2"
/>
<Button
android:id="@+id/btn3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn3"
/>
</LinearLayout>
</LinearLayout>

資源
復(fù)制代碼 代碼如下:

strings.xml
<resources>
<string name="app_name">LearningFragment</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_learn_fragment">LearnFragment</string>
<string name="layout1">LinearLayout1</string>
<string name="layout2">LinearLayout2</string>
<string name="fragment1">FragmentType1</string>
<string name="fragment2">FragmentType2</string>
<string name="num1">NO.1</string>
<string name="num2">NO.2</string>
<string name="num3">NO.3</string>
<string name="num4">NO.4</string>
<string name="btn1">Add fragment</string>
<string name="btn2">Replace fragment</string>
<string name="btn3">Remove fragment</string>
</resources>

程序運(yùn)行截圖
 
參考資料
API Guides:Fragments
http://developer.android.com/guide/components/fragments.html
FragmentManager類文檔:
http://developer.android.com/reference/android/app/FragmentManager.html
FragmentTransaction類文檔
http://developer.android.com/reference/android/app/FragmentTransaction.html

相關(guān)文章

  • Android 保存Fragment 切換狀態(tài)實(shí)例代碼

    Android 保存Fragment 切換狀態(tài)實(shí)例代碼

    本文主要介紹Android Fragment的應(yīng)用,這里給大家用實(shí)例代碼詳細(xì)介紹了Android Fragment 切換狀態(tài),有需要的小伙伴可以參考下
    2016-07-07
  • 模擬按Home鍵退出應(yīng)用的簡單方法(分享)

    模擬按Home鍵退出應(yīng)用的簡單方法(分享)

    下面小編就為大家?guī)硪黄M按Home鍵退出應(yīng)用的簡單方法(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-04-04
  • Android編程實(shí)現(xiàn)activity dialog透明背景的方法

    Android編程實(shí)現(xiàn)activity dialog透明背景的方法

    這篇文章主要介紹了Android編程實(shí)現(xiàn)activity dialog透明背景的方法,涉及Activity相關(guān)屬性設(shè)置及配置文件操作技巧,需要的朋友可以參考下
    2017-07-07
  • 一篇文章弄懂kotlin的擴(kuò)展方法

    一篇文章弄懂kotlin的擴(kuò)展方法

    這篇文章主要給大家介紹了如何通過一篇文章弄懂kotlin的擴(kuò)展方法,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用kotlin具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Android調(diào)用外置攝像頭的方法

    Android調(diào)用外置攝像頭的方法

    這篇文章主要為大家詳細(xì)介紹了Android調(diào)用外置攝像頭的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Kotlin?object的幾種用法示例詳解

    Kotlin?object的幾種用法示例詳解

    這篇文章主要為大家介紹了Kotlin?object的幾種用法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • Android實(shí)現(xiàn)動(dòng)態(tài)添加數(shù)據(jù)與堆疊折線圖詳解流程

    Android實(shí)現(xiàn)動(dòng)態(tài)添加數(shù)據(jù)與堆疊折線圖詳解流程

    堆疊折線圖是折線圖的一種,堆積折線圖用于顯示每一數(shù)值所占大小隨時(shí)間或有序類別而變化的趨勢,可能顯示數(shù)據(jù)點(diǎn)以表示單個(gè)數(shù)據(jù)值,也可能不顯示這些數(shù)據(jù)點(diǎn)。堆疊折線圖中,類別數(shù)據(jù)沿水平軸均勻分布,所有值數(shù)據(jù)沿垂直軸均勻分布
    2021-10-10
  • Flutter自定義Appbar搜索框效果

    Flutter自定義Appbar搜索框效果

    這篇文章主要為大家詳細(xì)介紹了Flutter自定義Appbar搜索框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • 詳解Flutter中Dart集合使用教程

    詳解Flutter中Dart集合使用教程

    集合是應(yīng)用程序中最為常見的數(shù)據(jù)結(jié)構(gòu),Dart一共支持四種集合,其中核心的List,?Map和Set在基礎(chǔ)框架中。本文將詳細(xì)講解Dart集合的最佳實(shí)踐,需要的可以參考一下
    2022-05-05
  • Android使用Canvas繪制圓形進(jìn)度條效果

    Android使用Canvas繪制圓形進(jìn)度條效果

    這篇文章主要為大家詳細(xì)介紹了Android使用Canvas繪制圓形進(jìn)度條效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10

最新評論