什么时候,附加活动在活动之间变为空?

时间:2017-02-13 17:01:55

标签: android android-intent android-activity

我在活动之间收到意图额外时遇到了问题。

在我的MainActivity中,我开始使用Gallery活动来选择外部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);
    }
  }

然后在我的GalleryView活动中:

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

即使我将extras = intent.getExtras()调用放入onCreate,它也永远不会被调用,因为它从未通过extras != null检查

我如何修复它(感谢STEFAN的回答)

所以我的主要问题是每当我开始新的Gallery Activity时,我的主要活动总是被设置为背景。我的清单文件规定它将在主活动上执行android:launchMode="singleTask"。因此,当主要活动重新启动时,意图从未真正传递,因为应用程序始终在后台运行并且从未传递意图附加内容。所以我尝试了onNewIntent()方法调用,并在那里尝试了一段代码来接收附加内容,并且它有效!再次感谢Stefan!

1 个答案:

答案 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
}