从另一个活动通过故意关闭活动 [英] Close activity from another activity through an intent
本文介绍了从另一个活动通过故意关闭活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从第一个打开的活动,我想缩小与意图的第一个。我想这一点,但接收器无法正常工作。而我在我的应用程序不同的接收器,所以我想,这个意图是从FirstReceiver只收到。我怎样才能做到这一点?
公共类首先扩展活动
{
@覆盖
保护无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_first);
意图接近=新的意图(getApplicationContext(),Close.class);
startActivity(关闭);
}
@覆盖
公共布尔onCreateOptionsMenu(菜单菜单)
{
//充气菜单;这增加了项目操作栏,如果它是present。
。getMenuInflater()膨胀(R.menu.first,菜单);
返回true;
}
类FirstReceiver扩展的BroadcastReceiver
{
@覆盖
公共无效的onReceive(上下文的背景下,意图意图)
{
Log.e(FirstReceiver,FirstReceiver);
First.this.finish();
}
}
}
这是第二类。
公共类关闭扩展活动
{
@覆盖
保护无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_close);
意图myIntent =新意图();
sendBroadcast(myIntent);
Log.e(的onCreate,的onCreate);
完();
}
@覆盖
公共布尔onCreateOptionsMenu(菜单菜单)
{
//充气菜单;这增加了项目操作栏,如果它是present。
。getMenuInflater()膨胀(R.menu.close,菜单);
返回true;
}
}
解决方案
这可以帮助你......
公共类首先扩展活动{
公共静态最后弦乐ACTION_CLOSE =yourPackageName.ACTION_CLOSE;
私人FirstReceiver firstReceiver;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_first);
IntentFilter的过滤器=新的IntentFilter(ACTION_CLOSE);
firstReceiver =新FirstReceiver();
registerReceiver(firstReceiver,过滤器);
意图接近=新的意图(getApplicationContext(),Close.class);
startActivity(关闭);
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
//充气菜单;这增加了项目操作栏,如果它是present。
。getMenuInflater()膨胀(R.menu.first,菜单);
返回true;
}
@覆盖
保护无效的onDestroy(){
super.onDestroy();
unregisterReceiver(firstReceiver);
}
类FirstReceiver扩展的BroadcastReceiver {
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
Log.e(FirstReceiver,FirstReceiver);
如果(intent.getAction()。等于(ACTION_CLOSE)){
First.this.finish();
}
}
}
}
公共类关闭延伸活动{
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_close);
意图myIntent =新的意图(First.ACTION_CLOSE);
sendBroadcast(myIntent);
Log.e(的onCreate,的onCreate);
完();
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
//充气菜单;这增加了项目操作栏,如果它是present。
。getMenuInflater()膨胀(R.menu.close,菜单);
返回true;
}
}
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;
}
}
这篇关于从另一个活动通过故意关闭活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文