簡體   English   中英

通過意圖關閉來自其他活動的活動

[英]Close activity from another activity through an intent

我想打開第一個活動,我想要有意圖關閉第一個活動。 我嘗試過這個,但是接收器不起作用。 我的應用程序中有不同的接收器,所以我希望這個意圖僅從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;
    }
}

這可能對你有所幫助......

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

因為最好使用startActivityForResult,onActivityRsult()

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

並覆蓋Activity onActivityResult並實現如下..

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

super.onActivityResult(requestCode, resultCode, data);     

 if (requestCode == RESULT_CLOSE){

       finish();
     }

如果要關閉mainActivity,請在Close活動中調用setResult()

當用戶希望退出所有打開的活動時,他們應該按下一個按鈕,加載應用程序啟動時運行的第一個活動,在我的情況下為“關閉”。

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

上面的代碼清除了除Close之外的所有活動。 Close是用戶運行程序時啟動的第一個活動。 然后將此代碼放入Close的onCreate中,以指示何時傳遞'Exit'消息時它應該自毀。

if (getIntent().getBooleanExtra("EXIT", false)) {
    finish();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM