我正在制作一款有三项活动的应用。用户可以按下按钮(主页,图形和写入)导航到新活动。这部分工作正常,但我只希望最多创建三个活动。现在,如果我按下按钮10次,我会进行10次单独的活动。有没有办法防止这种情况并让按钮调用一个活动,如果它已经创建而不是每次都创建一个新活动?
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button_toGraph = (Button) findViewById(R.id.home_to_graph);
button_toGraph.setOnClickListener(goToSecondListener);
Button button_toWrite = (Button) findViewById(R.id.home_to_write);
button_toWrite.setOnClickListener(goToThirdListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private OnClickListener goToSecondListener = new OnClickListener(){
@Override
public void onClick(View arg0) {
doButton();
}};
private void doButton()
{
startActivity(new Intent(this, GraphScreen.class));
}
private OnClickListener goToThirdListener = new OnClickListener(){
@Override
public void onClick(View v) {
doBacktoThird();
}
private void doBacktoThird() {
startActivity(new Intent(MainActivity.this, WriteScreen.class));
}
};
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
任何帮助将不胜感激!谢谢!
答案 0 :(得分:0)
创建三个布尔变量。
如果点击button_toGraph,请点击
Boolean button_toGraph_IsCreated = true;
当活动完成时(YourActivity.finish)再次将其设为false。
使用这些变量(如
)检查按钮if(button_toGraph_IsCreated) // if the variable is true
{
// do nothing as already activity created
}
else
{
doBacktoThird();
}
如果您正在使用数据库,那么获取主要活动的值应该不难。