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

标签 java android android-intent android-activity

我确定这是我对 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/

相关文章:

java - wifi 无法在菜单选项上打开/关闭

android - 如何在android中以编程方式启用位置访问?

java.lang.NoSuchMethodError : com. squareup.kotlinpoet.PropertySpec$Companion.varBuilder

java - 如何在暂停时重新启动整个应用程序?

java - 打开文件时未找到 Activity 异常 android

java - 操作 mongodb 中与正则表达式搜索匹配的字符串

java - 如何从自定义 Gson JsonSerializer 调用另一个序列化程序?

java - 如何使用 BufferedReader 向上移动 .txt 文件中的行?

java - 将 bean 包含到应用程序上下文中

android - Intent.ACTION_VIEW 视频 : external storage video "Sorry, this video cannot be played", 但在普通的安卓播放器中,它是可以播放的