Question

Trying to make a simple game app for android, having trouble setting up options.

Have a pretty basic set up right now.

Made a global class, since the only time it would ever really be changed is in the option menus, and the only time it would ever be read is when the game starts.

Player.java

package com.example.gametest;

import android.app.Activity;

public class player extends Activity
{
    int pID;
    String PlayerName;

    public player()
    {

    }

    public void setpID( int ID){pID = ID;}
    public int getpID (int ID){return pID;}
    public void setPlayerName(String Name){PlayerName = Name;}
    public String getPlayerName (int ID){return PlayerName;}
}

MainActivity.java

public class MainActivity extends Activity {
player player1 = new player();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    player1.pID = 1;
    player1.PlayerName = "NoName";
    setContentView(R.layout.activity_main);
    TextView textbox1 = (TextView) findViewById(R.id.textView1);
    textbox1.setText("Hello " + player1.PlayerName);
}
    public void optionsGame(View view) throws FileNotFoundException 
{
    // Do something in response to button
    Intent intent = new Intent(this, options.class);
    //EditText editText = (EditText) findViewById(R.id.editText1);
    //String message = editText.getText().toString();
    //intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);  
}
}

Options.java

import java.io.FileNotFoundException;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class options extends Activity{
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.optionpage);
        //Intent intent = getIntent();
    }

    public void backMain(View view) throws FileNotFoundException
    {
        super.onBackPressed();
    }

    public void saveOptions(View view) throws FileNotFoundException
    {
        TextView editText1 = (TextView) findViewById(R.id.editText1);
        Intent ActivityTwo = new Intent();

        Bundle bundle = new Bundle();

        bundle.putString("Player_Name", editText1.getText().toString());
        ActivityTwo.putExtras(bundle);
        setResult(RESULT_OK, ActivityTwo);
        finish();
    }
}

Basically, right now I just want to have it so that in the options activity, the name can be set there. Should be simple from what I've read, but can't get it.

I know why its wrong, nothing is ever really being set to a value that can travel between the Activity instance, but I have no idea how to make such a value.

Also, if you are woundering, in my xmls I use android:onClick="optionsGame" and android:onClick="saveOptions" .

So I looked at these, but still couldn't find an easy way to do this.

Sending data back to the Main Activity in android

how to pass values ​​between activity

Was it helpful?

Solution

I figured it out. MainActivity.java

    public void optionsGame(View view) throws FileNotFoundException 
{
    // Do something in response to button
    Intent intent = new Intent(this, options.class);
    Intent i = new Intent(this, options.class);
    startActivityForResult(i, 1);
    String message = player1.PlayerName;
    intent.putExtra(EXTRA_MESSAGE, message);  
}

and in Options.java

    public void saveOptions(View view) throws FileNotFoundException
{
    TextView editText1 = (TextView) findViewById(R.id.editText1);
    Intent returnIntent = new Intent();
     returnIntent.putExtra("result" , editText1.getText().toString());
     setResult(RESULT_OK,returnIntent);     
     finish();
}

and back in, create a new function. MainActivity.java

    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{

      if (requestCode == 1) 
      {

         if(resultCode == RESULT_OK)
         {      
             String result=data.getStringExtra("result");  
             player1.PlayerName = result;
             TextView textbox1 = (TextView) findViewById(R.id.textView1);
             textbox1.setText("Hello " + player1.PlayerName);
         }
         if (resultCode == RESULT_CANCELED) 
         {    
             //Write your code if there's no result
         }
      }
    }//onActivityResult
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top