gpt4 book ai didi

java - getIntent,无法让我的 Activity 获得最新的 Intent

转载 作者:行者123 更新时间:2023-11-30 11:07:09 30 4
gpt4 key购买 nike

我确定这是我对 Intent 的理解失败,但我有一个项目的 ExpandableListView,当我单击一个项目时,它会启动第一个项目,但每次之后它只会再次启动第一个项目,否无论我点击哪个。请求调试正常,但接收到的 Intent 总是像发送第一个 Intent 一样进行调试。经过半天的坚持,谷歌没有让我满意,我需要一些帮助。

Activity #1 Mainfest

<activity
android:name="com.h17.gpm.ActivityToDoList"
android:launchMode="singleInstance"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.h17.gpm.TODO" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

Activity #1 代码

Intent launch = new Intent(ActivityToDoList.this, ActivityToDoEdit.class);
launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
launch.putExtra("Action._ID", a.get_ID());
Log.d("ACTIVITYLAUNCHTEST", "Launch["+a.get_ID()+"]");
startActivity(launch);

Activity #2 Mainfest

<activity
android:name="com.h17.gpm.ActivityToDoEdit"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.h17.gpm.TODO.EDIT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

Activity 代码#2

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_todo_edit);
Intent i = getIntent();
Bundle extras = null;
if(i != null)
extras = i.getExtras();
if (extras != null){
action_id = extras.getLong("Action._ID");
Log.d("ACTIVITYLAUNCHTEST", "Receive["+action_id+"]");
}
}

我从其他帖子中读到 getIntent 返回第一个 Intent 所以也试过了

@Override
protected void onNewIntent(Intent intent){
Bundle extras = null;
if(intent != null)
extras = intent.getExtras();
if (extras != null){
action_id = extras.getLong("Action._ID");
Log.d("ACTIVITYLAUNCHTEST", "Receive New Intent["+action_id+"]");
}
setIntent(intent);
}

我还在 list 中尝试了很多 Intent Flags 和 Launch Modes 的组合,但对于我来说,第一次总是出现

Launch[1]
Receive[1]

第二次

Launch[2]
Receive[1]

从那时起,无论我发送什么值, Activity 都会以第一个值 1 启动,并且 onNewIntent 似乎永远不会触发。

生成 Intent 的完整函数

private void loadLists(){
ExpandableListView expandableList = (ExpandableListView) findViewById(R.id.expandableListViewToDoLists);
expandableList.setClickable(true);
adapter = new ActionListsExpandableAdapter(getApplicationContext());
expandableList.setAdapter(adapter);
expandableList.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {

Action a = (Action) parent.getExpandableListAdapter().getChild(groupPosition, childPosition);
if (startedForResult){
Intent data = new Intent();
data.putExtra("Action._ID", a.get_ID());
data.putExtra("Action.SUBJECT", a.getSUBJECT());
setResult(RESULT_OK, data);
finish();
}else{
ActionList al = (ActionList) parent.getExpandableListAdapter().getGroup(groupPosition);
Intent launch = new Intent(ActivityToDoList.this, ActivityToDoEdit.class);
launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
launch.putExtra("Action._ID", a.get_ID());
Log.d("ACTIVITYLAUNCHTEST", "Launching activity with intent for Action ID ["+a.get_ID()+"]");
launch.putExtra("ActionList._ID", al.get_ID());
launch.putExtra("ActionList.position", childPosition);
startActivity(launch);
}

return false;
}
});
}

最佳答案

可能这对您来说是一个很好的引用。在我看来,如果应用程序未被销毁,将调用类似 onNewIntent (with FLAG_ACTIVITY_SINGLE_TOP) 之类的 onCreate。希望对你有帮助。它适用于我的情况。

如需进一步引用,请参阅 Android: get most recent intent

关于java - getIntent,无法让我的 Activity 获得最新的 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29025495/

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