我有一个活动,上面有两个按钮。每个按钮打开一个新的布局。如果我将按回我的应用程序将关闭。但我想返回Mainactivity并且如果用户再次按下然后应用程序是关闭,如果他没有按下并转到第二个布局并按回然后应用程序不应该关闭。条件应该是如果用户处于Mainactivity并按下后退按钮然后应用程序关闭。 如果我可以设置这个条件,我想出了关于onBackPress方法的想法
if (k==1 && Its not a Mainactivity(or the user is not inside a Mainactivity)) {//I dont know how to check for that
Intent i = new Intent(this,Mainactivity.class);
startActivity(i);
this.finish();
} else if (k==1 && Its a Mainactivity(or the user is inside a Mainactivity)) {
this.finish(); //then i just want to close the application,I dont know this.finish() ,It does not close the app but still it runs in background
}
如果你们想在这里看到我的代码,这是我想要实现的一个简单例子: -
public class MainActivity extends AppCompatActivity {
Button btn1,button2; //Creating button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1= (Button)findViewById(R.id.btn1); //finding button1
button2=(Button)findViewById(R.id.button2); //finding button2
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setContentView(R.layout.btn1);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setContentView(R.layout.btn2);
}
});
}
}
请帮帮我。
答案 0 :(得分:2)
在onBackPressed()
中,只需检查k
的值即可。如果k
的值为1
或2
,那么
再次致电setContentView(R.layout.activity_main)
以显示主layout
,否则请致电super.onBackPressed()
以关闭MainActivity
。
每次设置initializeButtons()
布局后,您需要调用方法activity_main
,以便从布局中获取reference
按钮以及添加onClick
个侦听器。
尝试:
public class MainActivity extends AppCompatActivity {
Button button1, button2;
int k = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeButtons();
}
public void initializeButtons() {
button1 = (Button) findViewById(R.id.btn1);
button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
k = 1;
setContentView(R.layout.btn1);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
k = 2;
setContentView(R.layout.btn2);
}
});
}
@Override
public void onBackPressed() {
if (k == 1 || k == 2) {
k = 0;
setContentView(R.layout.activity_main);
initializeButtons();
} else {
super.onBackPressed();
}
}
}
<强>更新强>
根据{{1}}部分中的讨论,如果您想在comment
期间再次打开Mainctivity
,请按以下方式覆盖并更新back
:
onBackPressed()
希望这会有所帮助〜
答案 1 :(得分:0)
onBackPressed()
的默认行为是关闭当前Activity
。在您的情况下,只有一个Activity
,即MainActivity
,您在其中更改布局。
当您正在更改整个内容时,您也可以在那里创建新的Activity
和setContentView(R.layout.btn1)
。在onClick()
按钮中启动此新活动。您将获得所需的功能。
答案 2 :(得分:0)
您的问题是您应该为每项活动制作单独的布局。当您点击btn1
时,它将转到另一个活动。
但在您的代码中,您只需更改MainActivity
的内容。
因此,您应该使用Intent
在活动之间移动。
像这样:
Intent intent = new Intent (MainActivity.this, SecondAvtitity.class);
因此布局会发生变化,当您按back button
时,应用会返回MainActivity
。
当您在MainActivity
并按回按钮时,该应用将会关闭。
为了更好的解释,我做了一个示例应用程序:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ThirdActivity.class);
startActivity(intent);
}
});
}
}
答案 3 :(得分:0)
您可以维护已添加的所有先前布局的堆栈。首先添加一个实例变量:
this.subscription = this.route.params.subscribe(
(params: any) => {
this.currentSelectedSite = params['id'];
this.leaseTerminationService.fetchLeaseTerminationSite(this.currentSelectedSite)
.subscribe(
data => {
const myArray = [];
for (let key in data) {
myArray.push(data[key]);
}
this.selectedLeaseTermination = myArray;
}
)
//Make a call to get the SITE specific data for the selected site
this.sitesService.getSite(this.currentSelectedSite)
.subscribe(
data => {
const myArray = [];
for (let key in data) {
myArray.push(data[key]);
}
this.selectedSiteSite = myArray;
}
)
//Make a call to get the LEASE specific data for the selected site
this.leasesService.getLease(this.currentSelectedSite)
.subscribe(
data => {
const myArray = [];
for (let key in data) {
myArray.push(data[key]);
}
this.selectedSiteLease = myArray;
}
)
//Make a call to get the LEASE specific data for the selected site
this.leasesService.getLeasesList(this.currentSelectedSite)
.subscribe(
data => {
const myArray = [];
for (let key in data) {
myArray.push(data[key]);
}
this.selectedSiteLeases = myArray;
// Set the count of tabs
this.tabsCount = this.selectedSiteLeases.length;
this.tabs = [];
console.log('getLeasesList called');
this.createTabs(this.selectedSiteLeases);
// Set the first tab as the defaults after the tabs have been defined
this.setActiveTab(0);
}
)
}
)
}
public createTabs(leases) {
this.tabs = [];
for (let key in leases) {
this.tabs.push(
{title: 'Lease ' + this.selectedSiteLeases[key].lease_id
, content: this.selectedSiteLeases[key].renewal_latest_date }
);
}
}
然后,添加以下两种方法:
Stack<Integer> layoutHistoryStack = new Stack<>();
然后,使用 @Override
public void onBackPressed() {
if(layoutHistoryStack.size() == 1) {
// the first layout that was added is shown. finish()
layoutHistoryStack.pop();
finish();
return;
}
// pop() the current layout
layoutHistoryStack.pop();
// show the prev
setContentView(layoutHistoryStack.peek(), false);
}
public void setContentView(@LayoutRes int layoutResID, boolean addToHistoryStack) {
if(addToHistoryStack) layoutHistoryStack.add(layoutResID);
super.setContentView(layoutResID);
}
替换之前的每个setContentView(R.id.layout)
来电,即添加到setContentView(R.id.layout, true)
。
答案 4 :(得分:0)
这个怎么样?它确实有效。
我已完成Ferdous's snippet
:
public class MainActivity extends AppCompatActivity {
int k = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
k = 1;
setContentView(R.layout.activity_second);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
k = 2;
setContentView(R.layout.activity_third);
}
});
}
@Override
public void onBackPressed() {
if (k == 1 || k == 2) {
k = 0;
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
} else {
this.finishAffinity();
}
}
}