2016-10-14 105 views
0

活動之一(活動一)本人在申請時節省一個變量值顯示的視頻列表,你可以在圖片中看到:如何啓動另一個活動

enter image description here 的視頻存儲在一個名爲videosList的ArrayList,當用戶選擇一個視頻時,使用另一個活動B中的嵌入式YouTube播放器播放視頻。問題是當用戶從活動B(帶有視頻播放器的活動)返回到活動A(活動與視頻列表)變量videosList爲空,因此應用程序停止運行時出錯。

我試圖實現

protected void onSaveInstanceState(Bundle savedInstanceState) protected void onRestoreInstanceState(Bundle savedInstanceState)的方式來保存活動狀態以及一些變量,這樣當用戶返回到活動一遍應用程序就可以顯示視頻列表,但是當我嘗試gat我以前保存在onSaveInstanceState(Bundle savedInstanceState)中的值在public void onCreate(Bundle savedInstanceState)中,savedInstanceState始終爲NULL。

這裏是我的代碼:

public class VideosCatalogActivity extends AppCompatActivity implements AdapterView.OnItemClickListener { 



    private Toolbar toolbar; 
    private GridView videosGrid; 
    private ArrayList<VideoEntity> videosList; 

    private FirebaseAuth mAuth; 
    private FirebaseAuth.AuthStateListener mAuthListener; 
    private FirebaseDatabase database; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     Log.d(TAG, " onCreate(Bundle) - Ini "); 
     super.onCreate(savedInstanceState); 
     // onSaveInstanceState(); 

     setContentView(R.layout.videos_catalog_layout); 
     toolbar = (Toolbar) findViewById(R.id.app_bar); 
     setSupportActionBar(toolbar); 
     toolbar.setTitle(R.string.app_name); 
     toolbar.setTitleTextColor(getResources().getColor(R.color.com_facebook_button_background_color_focused)); 
     mAuth = FirebaseAuth.getInstance(); 

     mAuthListener = new FirebaseAuth.AuthStateListener() { 
      @Override 
      public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { 

       FirebaseUser user = mAuth.getCurrentUser(); 

      } 
     }; 

     database = FirebaseDatabase.getInstance(); 
     Bundle bundle = getIntent().getExtras(); 

     String actividad = bundle.getString("Activity"); 

     if (actividad.equals("DocumentariesCategoriesActivity")) { 


      videosList = bundle.getParcelableArrayList("com.app.example.VideoEntity"); 


updateCatalog(); 

      /* 

      String videoId = ""; 


      if (!videosList.isEmpty() && !videosList.equals(null)) { 

       for (VideoEntity video : videosList) { 
        DatabaseReference mRef = database.getReference().child("Videos").child(video.getId()); 
        mRef.setValue(video); 
       } 


       videosGrid = (GridView) findViewById(R.id.videosGrid); 
       MyGridViewAdapter adapter = new MyGridViewAdapter(this); 
       adapter.setVideosList(videosList); 
       videosGrid.setAdapter(adapter); 
       videosGrid.setOnItemClickListener(this); 
      } */ 

     } 


     Log.d(TAG, " onCreate(Bundle) - Fi "); 
    } 






    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     Log.d(TAG, "onCreateOptionsMenu(Menu) - Ini"); 

     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.main_menu, menu); 

     Log.d(TAG, "onCreateOptionsMenu(Menu) - Fi"); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem menuItem) { 

     Log.d(TAG, "onOptionItemSelected(MenuItem) - Ini"); 

     switch (menuItem.getItemId()) { 

      case R.id.action_logout: 
       updateActivity(mAuth.getCurrentUser()); 

     } 

     Log.d(TAG, "onOptionItemSelected(MenuItem) - Fi"); 
     return true; 
    } 


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

     Log.d(TAG, " onItemClick(AdapterView<?>, View, int, long) - Ini "); 

     Intent intent = new Intent(getApplicationContext(), YoutubePlayerActivity.class); 
     intent.putExtra("VIDEO_ID", videosList.get(position).getId()); 

     startActivity(intent); 

     Log.d(TAG, " onItemClick(AdapterView<?>, View, int, long) - Fi "); 

    } 

    protected void updateActivity(FirebaseUser user) { 
     Log.d(TAG, "updateActivity(FirebaseUser) - Ini"); 

     mAuth.signOut(); 
     Intent i = new Intent(VideosCatalogActivity.this, LoginActivity.class); 
     startActivity(i); 


     Log.d(TAG, "updateActivity(FirebaseUser) - Fi"); 
    } 

    @Override 
    protected void onSaveInstanceState(Bundle savedInstanceState) { 
     Log.d(TAG, "onSaveInstanceState(Bundle) - Ini"); 
     super.onSaveInstanceState(savedInstanceState); 

     savedInstanceState.putParcelableArrayList("VIDEOS_LIST", videosList); 
     savedInstanceState.putAll(savedInstanceState); 

     Log.d(TAG, "onSaveInstanceState(Bundle) - Fi"); 
     Log.i("","onSaveInstance is executed"); 
    } 

    @Override 
    protected void onRestoreInstanceState(Bundle savedInstanceState) { 
     Log.d(TAG, "onRestoreInstanceState(Bundle) - Ini"); 
     super.onRestoreInstanceState(savedInstanceState); 

     videosList = savedInstanceState.getParcelableArrayList("VIDEOS_LIST"); 
     updateCatalog(); 

     Log.i("","onRestoreInstance is executed"); 
     Log.d(TAG, "onRestoreInstanceState(Bundle) - Fi"); 
    } 

    protected void updateCatalog() { 

     Log.d(TAG, "updateCatalog() - Ini"); 

     String videoId = ""; 

     for (VideoEntity video : videosList) { 
      DatabaseReference mRef = database.getReference().child("Videos").child(video.getId()); 
      mRef.setValue(video); 
     } 

     videosGrid = (GridView) findViewById(R.id.videosGrid); 
     MyGridViewAdapter adapter = new MyGridViewAdapter(this); 
     adapter.setVideosList(videosList); 
     videosGrid.setAdapter(adapter); 
     videosGrid.setOnItemClickListener(this); 



     Log.d(TAG, "updateCatalog() - Fi"); 

    } 


} 

任何想法我怎麼能解決這個問題嗎?

+0

確保'VideoEntity'是'Parcelable'類。 – Shivansh

回答

0

爲什麼不使用SingletonData類?它存儲了ArrayList,當你需要重新加載ArrayList時,你可以從Singleton中加載它。

public class DataSingleton { 

    private static DataSingleton instance = new DataSingleton(); 
    private DataSingleton(){ } 

    public static DataSingleton getInstance(){ return instance; } 
    public static void setIntances(DataSingleton instance){DataSingleton.instance = instance;} 

    private ArrayList<Videos> videosList; 


    public void setArrayVideos(ArrayList<Videos> videos){ 
     videosList=videos; 
    } 

    public ArrayList<Videos> getArrayVideos(){ 
     return videosList; 
    } 
} 

然後您在活動A中調用類並將ArrayList設置爲您想要的位置。

DataSingleton.getInstance().setArrayVideos(videosList); 
videosList= DataSingleton.getInstance().getArrayVideos(); 
0

的onSaveInstanceState您實現必須這樣

@Override 
protected void onSaveInstanceState(Bundle savedInstanceState) { 
    Log.d(TAG, "onSaveInstanceState(Bundle) - Ini"); 
    if(savedInstanceState == null) 
    { 
     savedInstanceState = new Bundle(); 
    }   
    savedInstanceState.putParcelableArrayList("VIDEOS_LIST", videosList); 

    Log.d(TAG, "onSaveInstanceState(Bundle) - Fi"); 
    Log.i("","onSaveInstance is executed"); 

    super.onSaveInstanceState(savedInstanceState); 
} 
+0

它沒有工作,當我從Activity B返回Activity A時,savedInstanceState爲null – rainman

相關問題