Question

Currently when I click my button it uses the method startActivityForResult(); It finds the answer and returns.

The only problem is once it returns it will start the activity again. Personally I think its completely pointless to have a method that can only be run once. Surely there must be a flag that can be given to the method in order to tell it to run as many times as wanted?

I've read the javadoc and it doesn't seem to help because it says its a one time use, unless I'm reading it wrong?

IN MY SEARCH ACTIVITY:

private OnItemClickListener listListener = new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        String text = (String) ((TextView) arg1).getText();
        String[] selected = text.split(" - ");
        selected[0] = selected[0].replace(' ', '_');
        Log.w("COMPANY", selected[0]);
        Log.w("PART", selected[1]);

        Intent data = new Intent();
        data.putExtra("key", selected);
        setResult(RESULT_OK, data);

        finish();

        // startActivity(switch2);
    }

};

IN MY MAIN ACTIVITY (IN Button LISTENER)

if (search.isPressed() && searchPressed == false) {
            // show search list
            Intent switch1 = new Intent(MainActivity.this, SearchActivity.class);

            startActivityForResult(switch1, 0);

        }

@Override
protected void onActivityResult(int req, int resp, Intent data) {
    super.onActivityResult(req, resp, data);

    searchPressed = true;
    Bundle searched = data.getExtras();
    String[] newItem = searched.getStringArray("key");

    if (newItem[0].endsWith("_")) {
        handleXML(1);
        tv1.setText("Higher");
        tv2.setText("Lower");
    } else {
        handleXML(0);
        tv1.setText("Wear Resistance");
        tv2.setText("Tougher");
    }
    competitors = h.competitors;
    String[] piece = competitors.findCompanyParts(newItem);
    assignMaterials(piece);

    window.setVisibility(VISIBLE);
    grade.setVisibility(INVISIBLE);
    geo.setVisibility(INVISIBLE);
    s1.setVisibility(INVISIBLE);
    s2.setVisibility(INVISIBLE);
    search.setVisibility(INVISIBLE);
    help.setVisibility(INVISIBLE);
    myTabHost.setCurrentTab(0);
}
Was it helpful?

Solution

You are definitely doing something wrong. The method simply launches another activity. There is no real difference between startActivity() and startActivityForResult() other than that the second one has the option of being attached to a callback that will listen for some returned values from the launched Activity once that one has finished. You should paste in some code to show us what you're doing and I'm sure we'll quickly point out where you've gone wrong.

OTHER TIPS

You need to understand that its a callback method used to get the result in between the activities.
So there is no point of you being able to set how many times it should be called.
But if you want to reuse the data you got in it, then save that intent/data you get in it the first time and do whatever you wish.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top