gpt4 book ai didi

android - 什么时候 Intent 额外在 Activity 之间变为空?

转载 作者:行者123 更新时间:2023-11-30 00:43:22 31 4
gpt4 key购买 nike

我在接收 Activity 之间的额外 Intent 时遇到问题。

在我的 MainActivity 中,我启动了一个画廊 Activity 来选择外部 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 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 Activity 时,我找到了 extra,但是它是空的!来自 logcat:

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

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


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

所以我的主要问题是,每当我启动新的 Gallery Activity 时,我的 Main Activity 总是被设置为后台。我的 list 文件指示它将在主 Activity 上执行 android:launchMode="singleTask"。因此,不知何故,当 Main Activity 重新启动时, Intent 从未真正传递,因为该应用程序始终在后台运行并且从未传递 Intent 附加信息。所以我尝试了 onNewIntent() 方法调用并尝试了其中的一段代码来接收额外的东西,它成功了!再次感谢 Stefan!

最佳答案

您正在做的是在打开画廊后再次调用 MainActivity。根据 list 中使用的标志,这可能会导致您的主要 Activity 没有第二次启动,但您的初始主要 Activity 未暂停并提升到前台。

如果是这种情况,由于activity生命周期的原因,onCreate(...)不会再被调用,但是你可以检查下面的方法是否称为:

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

关于android - 什么时候 Intent 额外在 Activity 之间变为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42209513/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com