2017-02-13 48 views
0

我在接收活動之間的意向附加費時遇到問題。活動之間的意向附加項何時變爲空?

在我的MainActivity我開始一個畫廊活動選擇在外置SD卡的視頻文件:

public class MainMenu extends Activity { 
    //Button change video 
    Button video_change; 
    //Extra for changing video content 
    Bundle extras; 
    //Intent for Gallery view activity 
    Intent intent; 

    @Override 
    protected void onCreate(final Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    //... 
    intent = new Intent(getApplicationContext(),GalleryView.class); 

    video_change.setOnClickListener(new View.OnClickListener() { 

        @Override 
        public void onClick(View v) { 

         Uri mUri = null; 
         try { 
          Field mUriField = VideoView.class.getDeclaredField("mUri"); 
          mUriField.setAccessible(true); 
          mUri = (Uri) mUriField.get(myVideoView);   
         } catch(Exception e) { 
          //TODO: Something here 
         } 
         String string = mUri.toString(); 
         intent.putExtra("old_video",string); 
         startActivity(intent);    
        } 
       }); 
     } 

    @Override 
    public synchronized void onResume() { 
     super.onResume(); 
     if (Config.DEBUG) 
      Log.d(CLASS_NAME, "+ ON RESUME +"); 
     try { 
      extras = intent.getExtras(); 
      if (extras != null){ 
       Log.d("++ ON RESUME ++","Found Extra!"); 
       String newvideo = extras.getString("new_video"); 
       Log.d("++ ON RESUME ++","NEW VIDEO: "+ newvideo); 
       Uri tempuri = Uri.parse(newvideo); 
       myVideoView.setVideoURI(tempuri);   
      } 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     View decorView = getWindow().getDecorView(); 
     decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE 
       | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 
       | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 
       | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION 
       | View.SYSTEM_UI_FLAG_FULLSCREEN 
       | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); 
    } 
    } 
我圖庫視圖活動

然後:

public class GalleryView extends Activity { 

    ImageView back_button; 
    ListView videolist; 
    List<String> videos = new ArrayList<String>(); 
    private File[] videoFiles; 
    //private Cursor videocursor; 
    //private int video_column_index; 
    int x=0; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.gallery); 
     back_button = (ImageView) findViewById(R.id.back_button); 
     videolist = (ListView) findViewById(R.id.listview); 

     start_listview(); 

     ArrayAdapter adapter = new ArrayAdapter<String>(this,R.layout.activity_listview,videos); 
     videolist.setAdapter(adapter); 

     videolist.setOnItemClickListener(new OnItemClickListener(){ 
       @Override 
       public void onItemClick(AdapterView<?> adapter, View v, int position, long arg3) { 
        String value = (String)adapter.getItemAtPosition(position); 
        Log.d("VALUE: ",value); 
        Intent i = new Intent(getApplicationContext(),MainMenu.class); 
        // Send the file path 
        i.putExtra("new_video", value); 
        startActivity(i); 
       } 
     }); 

     back_button.setOnClickListener(new OnClickListener() { 
      //videolist.setAdapter(new VideoAdapter(getApplicationContext())); 
      @Override 
      public void onClick(View v) { 
      //videolist.setOnItemClickListener(videogridlistener); 
       Intent intent = new Intent(getApplicationContext(),MainMenu.class); 
       Bundle extras = getIntent().getExtras(); 
       String newString = extras.getString("old_video"); 
       intent.putExtra("new_video", newString); 
       startActivity(intent); 

      } 
     }); 
    } 

    public void start_listview() { 

     String path = ("/storage/extsd/Videos/"); 
     File directory = new File(path); 
     videoFiles = directory.listFiles(); 
     try { 
      for (File f : videoFiles) { 
       Log.d("FILE: ", f.toString()); 
       String file = f.toString(); 
       Uri tempuri = Uri.fromFile(f); 
       videos.add(file); 
      } 
      //Set the visibility of the progress bar to false. 
      findViewById(R.id.relativelayout_progress).setVisibility(View.GONE); 
     } catch (Exception e) { 
      // TODO: handle exception 
     } 
    } 
} 

的問題是,我的時候就回國了到MainMenu活動中,我找到了額外的東西,但它是空的!從logcat的:

MainMenu   + ON RESUME + 
++ ON RESUME ++ Found Extra! 
++ ON RESUME ++ NEWVIDEO: null 

即使我把在OnCreate的extras = intent.getExtras()調用,它永遠不會被調用,因爲它從來沒有經過extras != null檢查


我怎麼固定它(感謝Stefan的回答)

所以我的主要問題是我的主要活動總是被設置爲背景,每當我開始新的畫廊活動。我的清單文件表明它將在主要活動上執行android:launchMode="singleTask"。所以,不管怎樣,當主活動重新開始時,意圖從未真正通過,因爲應用程序總是在後臺運行,並且從未傳遞意圖附加組件。所以我嘗試了onNewIntent()方法調用,並試圖在那裏接收額外的代碼,它的工作!再次感謝Stefan!

+0

使用'startActivityForResult'和檢索值'onActivityResult' https://developer.android.com/training/basics/intents/result.html –

+0

我試過一次。我會再試一次 –

+0

和以前一樣的結果,它找到了額外的,但字符串爲空。 –

回答

1

您正在做的是打開圖庫後再打電話MainActivity。根據清單中使用的標誌,這可能會導致您的主要活動不會再次啓動,但是您的最初主要活動未被暫停並被提升到前臺。

如果是這種情況,由於活動的生命週期,onCreate(...)不會被再次調用,您可以檢查以下的方法被稱爲:

@Override 
protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 
    //TODO: check here if your intent extras arrive and log, then debug 
} 
+0

啊是的。我在我的清單中看到,我將activity設置爲'android:launchMode =「singleTask」'。我會嘗試看看onNewIntent如何工作。 –

+0

在OnNewIntent中放置日誌,但它永遠不會被調用。 –

+0

我能夠使用你的方法得到這個工作!我改變了在MainMenu中的onActivityResult和'Gallery Activity中的'setResult();'並且只是使用onNewIntent方法,而不是通過添加額外元素並使用'startActivity(intent);' –