活动之间何时额外的意图额外费用变为零? [英] When do intent extras become null between activities?

查看:70
本文介绍了活动之间何时额外的意图额外费用变为零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在两次活动之间不能收到意向额外的奖励.

I'm having problems receiving intent extras between activities.

在MainActivity中,我启动一个Gallery活动,以选择外部SD卡上的视频文件:

In my MainActivity I start a Gallery activity to chose video files on external SD card:

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活动中:

Then in my GalleryView activity:

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:

The problem is, I when I return back to the MainMenu activity, I the extra is found, but is null! From logcat:

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

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

Even if I put the extras = intent.getExtras() call in the onCreate, it never gets called because it never passes the extras != null check

我如何解决此问题(感谢STEFAN的答案)

所以我的主要问题是,每当我开始新的Gallery Activity时,我的Main Activity总是被设置为背景.我的清单文件要求对主活动执行android:launchMode="singleTask".因此,以某种方式重新启动主要活动时,意图从未真正通过,因为该应用程序始终在后台运行并且从未通过意图附加内容.因此,我尝试了onNewIntent()方法调用,并尝试了其中的代码段来接收额外内容,并且它起作用了!再次感谢斯蒂芬!

So my MAIN problem was that my Main Activity was always being set to the background whenever I started the new Gallery Activity. My manifest file dictated that it would do a android:launchMode="singleTask" on the Main Activity. So, somehow when the Main Activity was re-started, the intent was never truly passed since the app was always running in the background and never passed the intent extras. So I tried the onNewIntent() method call and tried the piece of code in there to receive the extras, and it worked! Thanks again to Stefan!

推荐答案

您要做的是在打开图库后再次调用MainActivity.根据清单文件中使用的标志,这可能会导致您的主要活动没有启动第二个时间,但是您最初的主要活动没有被暂停并提升为前台.

What you are doing is to call the MainActivity again after opening the gallery. Depending on the flags used in your manifest, that might cause that your main activity is not launched a second tim, but that your initial main activity is unpaused and raised to foreground.

在这种情况下,由于活动生命周期的原因,不会再次调用onCreate(...)但是,您可以检查是否调用了以下方法:

If this is the case, due to the activity lifecycle, onCreate(...)will not be called again, but you can check if the following method is called:

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

这篇关于活动之间何时额外的意图额外费用变为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆