getIntent在MainActivity中不起作用。变得奇怪的行为

时间:2013-09-21 05:45:52

标签: android android-intent menuitem

我有一个MainActivity,我在onCreate()方法中定义了以下代码。目的是,当MainActivity获得额外的字符串“EXIT”然后显示Toast消息:

Intent current = getIntent();
if (current !=null && current.getStringExtra("EXIT") != null) {
    Toast.makeText(this, "exiting", Toast.LENGTH_LONG).show(); 
}       

此MainActivity在某些按钮上启动另一个活动“DayOne”,如:

public void processGo(View v){
    Intent i = new Intent(MainActivity.this,DayOne.class);
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    MainActivity.this.startActivity(i);
}

现在我在添加额外字符串“EXIT”后从“DayOne”返回到MainActivity。我在onOptionsItemSelected(MenuItem item)方法中执行此操作:

public boolean onOptionsItemSelected(MenuItem item){
if(item.getTitle().equals("Exit")){
    Intent i = new Intent(DayOne.this,MainActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.putExtra("EXIT", "EXIT");
    startActivity(i);
    finish();
}
return super.onOptionsItemSelected(item);
}

问题是,当使用额外的字符串“EXIT”从DayOne调用MainActivity时;我没有看到MainActivity中定义的Toast消息。这里缺少什么或错了什么?

感谢任何帮助。

4 个答案:

答案 0 :(得分:1)

感谢大家的评论和帮助。

我在这里已经找到了问题。这是因为清单文件为两个活动(MainActivity和DayOne Activity)输入了一个条目android:launchMode =“singleInstance”..

从那里删除它,它工作正常。

答案 1 :(得分:0)

首先检查您的null,然后获取Intent String,您是否可以在onAreate of MainActivity @

中使您的代码看起来如下所示
Bundle extra= getIntent().getExtras();
if(extra!=null){

 String _StrExit=extra.getString("EXIT");

 if(_StrExit.equalsIgnoreCase("EXIT")){
 Toast.makeText(this, "exiting", Toast.LENGTH_LONG).show(); 
    }
  }

<强>更新 从menuitem

调用Intent时进行更改
 public boolean onOptionsItemSelected(MenuItem item){
    if(item.getTitle().equals("Exit")){
        Intent i = new Intent(DayOne.this,MainActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        String _Str="EXIT";
        i.putExtra("EXIT", _Str);
        startActivity(i);
        finish();
    }

return super.onOptionsItemSelected(item);
}

答案 2 :(得分:0)

它为我工作。

<强> MainActivity:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent current = getIntent();
    if (current != null && current.getStringExtra("EXIT") != null) {
        Toast.makeText(this, "exiting", Toast.LENGTH_LONG).show();
    }
}

public void processGo(View view) {
    Intent i = new Intent(MainActivity.this, OneDayActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(i);
}

}

<强> OneDayActivity:

public class OneDayActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_day_one);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.day_one, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getTitle().equals("Exit")) {
        Intent i = new Intent(this, MainActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        i.putExtra("EXIT", "EXIT");
        startActivity(i);
        finish();
    }
    return super.onOptionsItemSelected(item);
}

}

答案 3 :(得分:0)

不要清除堆栈,而不是当你回到主要活动时想要执行某项任务(使用“EXIT”字符串),你也可以使用onActivityResult来实现。:

 public void processGo(View v){
        Intent i = new Intent(MainActivity.this,DayOne.class);
        MainActivity.this.startActivity(i,10); // 10 is the id to handle
    }



 @Override
    public void onActivityResult( int requestCode, int resultCode, Intent data )
    {
        super.onActivityResult( requestCode, resultCode, data );
        switch ( requestCode )
        {
            case ( 10 ): // id that we pass on start activity
            {
                if ( resultCode == Activity.RESULT_OK )
                {


                 Toast.makeText(this, "exiting"+data.getStringExtra( "EXIT", "" ),  Toast.LENGTH_LONG).show(); 
                }
            }
            break;
       }
  }

完成

public boolean onOptionsItemSelected(MenuItem item){
if(item.getTitle().equals("Exit")){
        Intent resultIntent = new Intent();
        resultIntent.putExtra( "EXIT", "EXIT" );
        setResult( Activity.RESULT_OK, resultIntent );
        finish();
}
return super.onOptionsItemSelected(item);
}

完成后,它将返回主要活动并自动致电onActivityResult,您可以在那里完成任务。

抱歉打字错误希望它有所帮助。