简体   繁体   中英

Close activity from another activity through an intent

I want to open an activity from the first one, and i want to close the first one with an intent. I tried this, but the receiver doesn't work. And i have different receivers in my application, so i want that this intent is received only from FirstReceiver. How can i do it?

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

And this is the second class.

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

this may help you...

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

For it is better to Use startActivityForResult,onActivityRsult()

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

and override onActivityResult in your Activity and implement like this..

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

super.onActivityResult(requestCode, resultCode, data);     

 if (requestCode == RESULT_CLOSE){

       finish();
     }

call the setResult() in Close activity when ever you want to close the your mainActivity..

When the user wishes to exit all open activities, they should press a button which loads the first Activity that runs when your app starts, in my case "Close".

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

The above code clears all the activities except for Close . Close is the first activity that is brought up when the user runs the program. Then put this code inside the Close 's onCreate, to signal when it should self destruct when the 'Exit' message is passed.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM