문제

I am making an app that will has three activities. The user can navigate to a new activity pressing buttons (home, graph, and write). This part works fine, but I only want three activities to be created max. Right now if I push the buttons 10 times I get 10 separate activities. Is there a way to prevent this and have the button call an activity if it has already been created instead of creating a new one each time?

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;
    }
}

Any help will be greatly appreciated! Thank you!

도움이 되었습니까?

해결책

create three Boolean Variables. if suppose button_toGraph is clicked .. make the Boolean button_toGraph_IsCreated = true;

and when Activity is Finished ( YourActivity.finish ) make it Again false.

make a check on the button by using these variable like

if(button_toGraph_IsCreated) // if the variable is true
{
// do nothing as already activity created
}
else
{
 doBacktoThird();
}

if you are using database then it should not be hard to get the values to main activity.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top