5

我想从第一个活动中打开一个活动,并且我想有意识地关闭第一个活动。我试过这个,但接收器不起作用。而且我的应用程序中有不同的接收器,所以我希望这个意图只从 FirstReceiver 接收。我该怎么做?

public class First extends Activity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        Intent close = new Intent(getApplicationContext(), Close.class);
        startActivity(close);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.first, menu);
        return true;
    }

    class FirstReceiver extends BroadcastReceiver 
    {
        @Override
        public void onReceive(Context context, Intent intent) 
        {
            Log.e("FirstReceiver","FirstReceiver");
            First.this.finish();
        }
    }
}

这是第二课。

public class Close extends Activity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_close);
        Intent myIntent = new Intent();
        sendBroadcast(myIntent);
        Log.e("onCreate","onCreate");
        finish();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.close, menu);
        return true;
    }
}
4

3 回答 3

10

这可能会帮助你...

public class First extends Activity {
    public static final String ACTION_CLOSE = "yourPackageName.ACTION_CLOSE";
    private FirstReceiver firstReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        IntentFilter filter = new IntentFilter(ACTION_CLOSE);
        firstReceiver = new FirstReceiver();
        registerReceiver(firstReceiver, filter);
        Intent close = new Intent(getApplicationContext(), Close.class);
        startActivity(close);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.first, menu);
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(firstReceiver);
    }

    class FirstReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.e("FirstReceiver", "FirstReceiver");
            if (intent.getAction().equals(ACTION_CLOSE)) {
                First.this.finish();
            }
        }
    }
}

public class Close extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_close);
        Intent myIntent = new Intent(First.ACTION_CLOSE);
        sendBroadcast(myIntent);
        Log.e("onCreate", "onCreate");
        finish();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.close, menu);
        return true;
    }
}
于 2013-11-08T13:39:03.727 回答
2

因为最好使用 startActivityForResult,onActivityRsult()

 Intent in = new   Intent(getApplicationContext(), Close.class);
 startActivityForResult(in, RESULT_CLOSE);

并覆盖onActivityResult你的Activity并像这样实现..

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  

super.onActivityResult(requestCode, resultCode, data);     

 if (requestCode == RESULT_CLOSE){

       finish();
     }

setResult()当你想关闭你的 mainActivity 时调用in Close 活动。

于 2013-11-08T13:23:45.940 回答
1

当用户希望退出所有打开的活动时,他们应该按下一个按钮,该按钮会加载您的应用程序启动时运行的第一个活动,在我的例子中是“关闭”。

Intent intent = new Intent(getApplicationContext(), Close.class);
intent.putExtra("EXIT", true);
startActivity(intent);

上面的代码清除了除Close之外的所有活动。关闭是用户运行程序时启动的第一个活动。然后将此代码放在Close的 onCreate 中,以在传递“退出”消息时发出信号何时自毁。

if (getIntent().getBooleanExtra("EXIT", false)) {
    finish();
}
于 2013-11-08T13:30:31.240 回答