【问题标题】:Android Fragments OnCreate vs OnCreateViewAndroid 片段 OnCreate 与 OnCreateView
【发布时间】:2017-06-18 03:47:48
【问题描述】:

我有一个带有OnCreateViewonCreate 的片段。在onCreate() 我下载了一些图标并设置在ImageView。程序第一次运行没问题。图标已下载并设置,但只要我切换选项卡并返回 onCreateView() 就会被调用并将界面重置为相关 xml 文件中所描述的准确方式。

我想知道是否有可能阻止这件事发生。将代码从 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>

更新

阅读 cmets 以获得正确答案

【问题讨论】:

    标签: android android-fragments android-lifecycle


    【解决方案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);
                  }
                }
    

    【讨论】:

    • 不,不仔细看看
    • 不应该覆盖 Fragments 的非默认构造函数
    • 我同意你的看法,但 Android Studio 会建议我在我的手机上进行修复,所以我不记得正确的方法,但总的来说它会完成这项工作
    • 无论如何你都不需要将 Context 对象传递给 Fragment,所以我不确定这个答案是什么意思
    • 你这样做是因为它还没有创建,所以在那个状态你没有上下文
    猜你喜欢
    • 2011-12-23
    • 2016-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多