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;
}
}
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 .
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.
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..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With