Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
    }
}
like image 785
As As Avatar asked Nov 08 '13 13:11

As As


People also ask

How can an intent call another activity?

To start an activity, call startActivity() and pass it your Intent . The system receives this call and starts an instance of the Activity specified by the Intent .

How intent is used to link two activities?

Intent i = new Intent(FromActivity. this, ToActivity. class); startActivity(i); In this case the Intent uses your current Activity as the Context in the first parameter, and the destination Activity in the second parameter.


2 Answers

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;
    }
}
like image 96
Gopal Gopi Avatar answered Oct 05 '22 21:10

Gopal Gopi


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

like image 26
kalyan pvs Avatar answered Oct 05 '22 21:10

kalyan pvs