2017-06-18 77 views
0

我有一個片段OnCreateViewonCreate。在onCreate()我下載了一些圖標並設置在ImageView。程序第一次運行就沒問題。圖標被下載並設置,但只要我切換標籤並返回onCreateView()被調用,並重置接口到如何在相關的xml文件中描述。Android片段OnCreate vs OnCreateView

我想知道是否有可能阻止這件事發生。將代碼從onCreate移動到onCreateView並不是我想要的,因爲它只是一直重複下載圖標。

Fragment

public class LiveGameTab1Fragment extends Fragment { 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View V = inflater.inflate(R.layout.live_game_tab1, container, false); 
    return V; 
    } 

    @Override 
    public void onCreate(Bundle bundle) { 
    super.onCreate(bundle); 
    //download an icon from the internet 
    Bitmap bitmap = BitmapFactory.decodeByteArray(icon.getIcon(), 0, icon.getIcon().length); 
    Drawable iconDrawable = new BitmapDrawable(getResources(), bitmap); 
    imgButton.setImageDrawable(iconDrawable); 
    } 
} 

FragmentActivity

public class LiveGameStats extends FragmentActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_live_game_stats); 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

    if (savedInstanceState == null) { 
     getSupportFragmentManager().beginTransaction().add(R.id.liverealtabcontent, new LiveGameTab1Fragment()).commit(); 
     getSupportFragmentManager().beginTransaction().add(R.id.liverealtabcontent2, new LiveGameTab2Fragment()).commit(); 
     getSupportFragmentManager().beginTransaction().add(R.id.liverealtabcontent3, new LiveGameTab3Fragment()).commit(); 

    } 

    FragmentTabHost mTabHost = (FragmentTabHost) findViewById(R.id.liveGameTabhost); 
    mTabHost.setup(LiveGameStats.this, getSupportFragmentManager(), android.R.id.tabcontent); 
    mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("You"), 
      LiveGameTab1Fragment.class, null); 
    mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Your Team"), 
      LiveGameTab2Fragment.class, null); 
    mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Your Enemies"), 
      LiveGameTab3Fragment.class, null); 


    } 
} 

live_game_tab1.xml

<LinearLayout 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" 
    android:orientation="vertical" 
    tools:context=".DeviceFragment"> 

    <ImageButton 
    android:id="@+id/champBtn" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/button_shape_champ" 
    /> 

</LinearLayout> 

activity_live_game_stats.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_live_game_stats" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/holo_blue_light" 
    tools:context="lucian.leagueassistant.activity.LiveGameStats"> 


    <android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/liveGameTabhost" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <TabWidget 
      android:id="@android:id/tabs" 

      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0" 
      android:orientation="horizontal" /> 

      <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 


      <FrameLayout 
      android:id="@+id/liverealtabcontent" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" /> 
      <FrameLayout 
      android:id="@+id/liverealtabcontent2" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" /> 
      <FrameLayout 
      android:id="@+id/liverealtabcontent3" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" /> 

    </LinearLayout> 
</android.support.v4.app.FragmentTabHost> 

UPDATE

閱讀正確的答案的評論

回答

1

很抱歉,如果代碼看起來脫節,我從我的電話接聽

public class LiveGameTab1Fragment extends Fragment { 
      Bitmap bitmap; 
      Drawable iconDrawable; 

      public LiveGameTab1Fragment() {} 

      public LiveGameTab1Fragment(Context context 
      //add this if you need something from resources) { 
//I don't know what you mean by icon.getIcon() 
       bitmap = BitmapFactory.decodeByteArray(icon.getIcon(), 0, icon.getIcon().length); 

       iconDrawable = new BitmapDrawable(context.getResources(), bitmap); 
      } 

       @Override 
       public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
       View V = inflater.inflate(R.layout.live_game_tab1, container, false); 
       return V; 
       } 

       @Override //you need the onViewCreated function again I'm on my phone so I might be wrong about the variables inside so make sure to take the right code and the functions inside 
       public void onViewCreated(View v) { 
       super.onViewCreated(v); 
       //code moved to constructor so the image is initialize only ones 


       ImageButton imgButton = (ImageButton) v.findViewById(R.id.yourButtonId); 

       imgButton.setImageDrawable(iconDrawable); 
       } 
      } 
+0

不,這不是仔細看看 –

+1

你不應該覆蓋碎片的非默認構造函數 –

+0

我同意你的意見,但Android工作室將p ropose修復我在我的手機上,所以我不記得正確的方式,但總的來說它會完成這項工作 –