Green_qaue
Green_qaue

Reputation: 3661

startActivityForResult(), how does it work?

Im working on my level-selector activity right now and I want to get the result from wich level I chose in my MainGameView, that is run by another activity. How would I go about doing this?

This is what Ive for when you choose level 1:

     Button test1 = (Button)findViewById(R.id.test1);
     test1.setOnClickListener(new OnClickListener() {

       public void onClick(View v) {
            setLevel(1);
            Intent start = new Intent("nielsen.happy.activities.HAPPYACTIVITY");
            startActivityForResult(start, getLevel());
            finish();
        }
    });

but where do I go from here? How do I get this value into my view so I can change bitmaps and other values depending on what level they chose? Is there something I need to do in my "HappyActivity"(MainGameAcitvity) to get the result? cus right now its returning 0

explenation of how games set up: Menu -> levelselector -> Game. So I need to get result from levelselector into game, so it knows what level to start.

Upvotes: 0

Views: 998

Answers (1)

Cookster
Cookster

Reputation: 1463

(updated corrected response) Don't use startActivityForResult().

Try this for Activity A:
Intent start = new Intent("nielsen.happy.activities.HAPPYACTIVITY"); start.putExtra("level", getLevel());
startActivity(start);
finish();

Then in Activity B, do this:
Extras mBundle = getIntent().getExtras();
int level = mBundle.getInt("level");

(original, incorrect response) Lets call activity A the one that houses your sample code above. Lets call activity B the one that you refer to as HAPPACTIVITY.

Activity A calls startActivityForResult() with a request code. Activity B starts up and before exiting, you call setResult(int code).

When activity B finishes, A returns to the top via the method onActivityResult().

Implement an onActivityResult() and see what attributes you get.

FYI there is a condition where setResult(RESULT_OK) or setResult(RESULT_CANCELED) will not trigger onActivityResult() in A (I can't recall what the case is).

Upvotes: 1

Related Questions