问题描述
我的应用中有5个活动(A1,A2,A3,A4,A5)。每个活动具有一个文本视图和一个按钮(B1,B2,B3,B4,B5)。如果你点击该按钮后,进入到下一个活动。假设如果你点击了B1按钮,然后它去A2活性还有一件事每个活动都有一个菜单键(退出)如果你单击该按钮,然后将退出该应用程序。但它不工作。在这里,我用下面的code的每个活动调用。
My Application have 5 activities(A1,A2,A3,A4,A5). Each activity have one text view and one button(B1,B2,B3,B4,B5). If you click on that button then goes to next activity. suppose if you click on the B1 button then it goes to A2 activity and one more thing each activity have one menu button(Logout) if you click that button then it will exit from the application. But it is not working.Here i am using the following code for every activity calling.
对于明确的堆栈
Intent intent = new Intent(act1.this,act2.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
在退出按钮点击监听器,我使用完的完成()当前的活动
。因为我们都清楚使用堆栈 FLAG_ACTIVITY_CLEAR_TOP
现在堆栈包含当前活动仅这就是为什么我只是完成当前活动。但是,如果你点击退出按钮,它只是完成当前actvity只。它不是从应用程序退出。在这里,堆栈正在使用的语句清除或不 FLAG_ACTIVITY_CLEAR_TOP
。以下是我的code任何人可以帮助我。
In Logout button click listener, i finished current activity using the finish()
.because we are clear the stack using FLAG_ACTIVITY_CLEAR_TOP
now stack contains current activity only thats why i just finish the current activity. But if you click on the Logout button it just finish current actvity only. It not exit from the application. Here stack is cleared or not using that statement FLAG_ACTIVITY_CLEAR_TOP
. Following is my code can anybody help me.
Actvity一个
public class logout extends Activity
{
TextView tv;
Button next;
public static final int logout_menu = Menu.FIRST+1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.text);
tv.setText("activity1");
Button next = (Button) findViewById(R.id.button);
next.setOnClickListener(nextListener);
}
private OnClickListener nextListener = new OnClickListener()
{
public void onClick(View v)
{
try
{
Intent intent = new Intent(logout.this,act2.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
catch(Exception ex2)
{
System.out.println("Not able to launch Registration Screen"+ex2.toString());
}
}
};
public boolean onCreateOptionsMenu(Menu menu){
// TODO Auto-generated method stub
boolean result = super.onCreateOptionsMenu(menu);
menu.add(0, logout_menu, 0, "Logout");
return result;
}
public boolean onOptionsItemSelected(MenuItem item){
// TODO Auto-generated method stub
switch (item.getItemId()) {
case logout_menu:finish();
break;
}
return super.onOptionsItemSelected(item);
}
}
Actvity2
public class act2 extends Activity
{
TextView tv;
Button next;
public static final int logout_menu = Menu.FIRST+1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.text);
tv.setText("activity2");
Button next = (Button) findViewById(R.id.button);
next.setOnClickListener(nextListener);
}
private OnClickListener nextListener = new OnClickListener()
{
public void onClick(View v)
{
try
{
Intent intent = new Intent(act2.this,act3.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
catch(Exception ex2)
{
System.out.println("Not able to launch Registration Screen"+ex2.toString());
}
}
};
public boolean onCreateOptionsMenu(Menu menu){
// TODO Auto-generated method stub
boolean result = super.onCreateOptionsMenu(menu);
menu.add(0, logout_menu, 0, "Logout");
return result;
}
public boolean onOptionsItemSelected(MenuItem item){
// TODO Auto-generated method stub
switch (item.getItemId()) {
case logout_menu:finish();
break;
}
return super.onOptionsItemSelected(item);
}
}
感谢
推荐答案
您必须设置的setResult(INT结果code)
要在其中的活动登出。然后在previous活动,你必须抓住这个结果 onActivityResult(INT申请code,INT结果code,意图数据)
。在这里,你可以完成你的活动。再次捕捉在这里你可以的setResult收previous之一,同样的做法。例:
You have to set setResult(int resultCode)
on the activity where you want to logout. Then on previous Activity you have to capture this result in onActivityResult(int requestCode, int resultCode,Intent data)
. Here you can Finish your Activity. Again capturing here you can setResult to close previous one and same approach.Ex.:
您设置导致对退出菜单preSS为: 完(); //要完成您目前的acivity 的setResult(R.id.common_menu_logout);
You set result on logout menu press as: finish(); //To finish your current acivity setResult(R.id.common_menu_logout);
然后在previous活动:
Then on previous activity:
protected void onActivityResult(int requestCode, int resultCode,Intent data) {
switch(resultCode){
case R.id.common_menu_logout:
setResult(R.id.common_menu_logout);
closeActivity(); // to close this activity
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
这篇关于机器人 - 如何从应用程序注销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!