Reputation: 11
So I am making an android app where when you open the app, you start off in the main activity. Once you hit the play button, I made an onclick listener that sets the content view to another class that extends View. The game goes on in this class by drawing to a canvas. Once the user loses the game, I want it to reset the content view to my main activity. Is there any way to do this? Sorry if this is confusing, I am fairly new to java. Any help would be greatly appreciated. Thanks!
Upvotes: 1
Views: 439
Reputation: 13199
If you want to send information to another class from MainActivity
and after returns it initialized, you can use startActivityForResult()
, like this:
startActivityForResult(new Intent("Example"), 1);
where you have to declare your intent "Example"
in your Manifest
file:
<activity
android:name=".ExecuteExample" //Here the name of the Class in which you send the information
android:label="Example" >
<intent-filter>
<action android:name="Example" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
So, when the user loses the game, you retrieve the new information again to the MainActivity
with setResult
function (from ExecuteExample
):
setResult(RESULT_OK, example);
where example
it's the name of the intent that you send now to MainActivity
. How to set the information to this new Intent? Easy, just with .putExtra()
function. Something like this:
example.putExtra("Score",0);
example.putExtra("Name","");
supossing that the values that you want to initialize are the Score
and the Name
of the user that played before.
Finally, you have to retrieve this information initialized from the MainActivity
class creating a new method onActivityResult()
:
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == 1)
{
if(resultCode == RESULT_OK)
{
int score = data.getIntExtra("Score");
String name = datos.getStringExtra("Name");
}
}
}
I expect it will helps to you!
Upvotes: 1
Reputation: 447
Are you trying to load activity? You can try with the code below:
Intent intent = new Intent(this, MainActivity.class);
Upvotes: 0