getIntent,无法获取我的活动以获得最新的意图

时间:2015-03-13 05:30:45

标签: java android android-intent android-activity

我确定这是对我的意图的理解失败,但我有一个ExpandableListView项目,当我点击一个项目时,它启动第一个OK但是每次之后它只会再次启动第一个没有我点击的问题。请求调试为OK,但接收到的intent总是调试,就好像发送了第一个一样。经过半天坚持并且谷歌失败了,我需要一些帮助。

活动#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>

活动#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);

活动#2主要活动

<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>

活动代码#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);
}

我还在Manifest中尝试了许多Intent Flags和Launch Modes的组合,但是对于我的生活,第一次总是出现在

Launch[1]
Receive[1]

第二次

Launch[2]
Receive[1]

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

生成意图的完整函数

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;
        }
    });
}

1 个答案:

答案 0 :(得分:1)

可能这对您来说是一个很好的参考。 对我来说,如果应用程序没有被销毁,似乎会调用startForeground(1337, notification); 之类的onCreate。希望对你有所帮助。在我的情况下,它适用于我。

有待进一步参考,请参阅 Android: get most recent intent