[Java for Android App Development] squabhouse:
This is a rudimentary android application that serves as an umpires strike/ball/out counter. There is a settings icon in the action bar of the MainActivity. When this icon is 'clicked on', a new Activity is started consisting of a PreferenceFragment, which consists of a checkboxpreference, and a Fragment that consists of 3 buttons. These buttons reset the stike_count, ball_count and total_outs_count to zero. I have spent a day on this and am having trouble figuring out how to manipulate integer variables in my MainActivity from button clicks in a seperate Activity's Fragment. Please point me in the right direction.

package edu.umkc.baldwin;

import android.app.Activity;

import android.app.FragmentManager;

import android.app.FragmentTransaction;

import android.content.Context;

import android.content.Intent;

import android.content.SharedPreferences;

import android.os.Bundle;

import android.preference.PreferenceFragment;

import android.util.Log;

import android.view.Gravity;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.LinearLayout;

import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends Activity {

private static final String TAG = "UmpireActivity";

// Constant variables declared/initialized to keep track of values

// across lifecycle states

[MENTION=5448622]Suppress[/MENTION]Warnings("unused")

private static final String STRIKES = "0";

[MENTION=5448622]Suppress[/MENTION]Warnings("unused")

private static final String BALLS = "0";

[MENTION=5448622]Suppress[/MENTION]Warnings("unused")

private static final String OUTS = "0";

TextView strikeCounterTV;

TextView ballCounterTV;

TextView totalOutCounterTV;

private Button strikeCounterButton;

private Button ballCounterButton;

private int strike_count;

private int ball_count;

private int out_count;

private void updateViews(){

strikeCounterTV.setText(String.valueOf(strike_count));

ballCounterTV.setText(String.valueOf(ball_count));

totalOutCounterTV.setText(String.valueOf(out_count));

}

private void displayToast(boolean x){

int messageId = 0;

if (x == true){

messageId = R.string.strike_toast_view;

} else {

messageId = R.string.ball_toast_view;

}

Toast toast = Toast.makeText(MainActivity.this, messageId,

Toast.LENGTH_SHORT);

LinearLayout toastLayout = (LinearLayout) toast.getView();

TextView toastTV = (TextView) toastLayout.getChildAt(0);

toast.setGravity(Gravity.CENTER|Gravity.CENTER, 0, -150);

toastTV.setTextSize(42);

toast.show();

}

/*

* (non-Javadoc)

* [MENTION=690402]see[/MENTION] android.app.Activity#onCreate(android.os.Bundle)

*/

[MENTION=439709]override[/MENTION]

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

Log.d(TAG, "OnCreate(Bundle) called");

setContentView(R.layout.main_page_layout);

//Declare TextView objects and Button objects

strikeCounterTV = (TextView) findViewById(R.id.strikeCountTextView);

ballCounterTV = (TextView) findViewById(R.id.ballCountTextView);

totalOutCounterTV = (TextView) findViewById(R.id.totalOutsTextViewCounter);

strikeCounterButton = (Button) findViewById(R.id.strikeCountButton);

ballCounterButton = (Button) findViewById(R.id.ballCountButton);

strikeCounterButton.setOnClickListener(new OnClickListener(){

[MENTION=439709]override[/MENTION]

public void onClick(View v) {

if (strike_count < 2){

strike_count++;

updateViews();

} else {

// batter has reached strike limit

displayToast(true);

out_count++;

strike_count = 0;

ball_count = 0;

updateViews();

}

}

});

ballCounterButton.setOnClickListener(new OnClickListener(){

[MENTION=439709]override[/MENTION]

public void onClick(View v) {

if (ball_count < 3){

ball_count++;

updateViews();

} else {

// batter has reached ball limit

displayToast(false);

strike_count = 0;

ball_count = 0;

updateViews();

}

}

});

// Check for screen rotation

if (savedInstanceState == null){

strike_count = 0;

ball_count = 0;

} else {

strike_count = savedInstanceState.getInt("STRIKES");

ball_count = savedInstanceState.getInt("BALLS");

}

// Save 'totalOuts' variable through application exit

SharedPreferences savedObject = getPreferences(Context.MODE_PRIVATE);

out_count = savedObject.getInt(getString(R.string.total_outs_key), 0);

updateViews();

}

[MENTION=439709]override[/MENTION]

public void onSaveInstanceState(Bundle savedInstanceState) {

super.onSaveInstanceState(savedInstanceState);

Log.d(TAG, "onSaveInstanceState() called");

savedInstanceState.putInt("STRIKES", strike_count);

savedInstanceState.putInt("BALLS", ball_count);

savedInstanceState.putInt("OUTS", out_count);

}

[MENTION=439709]override[/MENTION]

public boolean onCreateOptionsMenu(Menu menu) {

//Displays actionbar items in app actionbar

getMenuInflater().inflate(R.menu.actionbar_layout, menu);

return super.onCreateOptionsMenu(menu);

}

[MENTION=439709]override[/MENTION]

public boolean onOptionsItemSelected(MenuItem item){

super.onOptionsItemSelected(item);

switch (item.getItemId()){

case (R.id.reset_option):

strike_count = 0;

ball_count = 0;

updateViews();

return true;

case (R.id.about_menu_option):

Intent i = new Intent(this, About.class);

startActivity(i);

return true;

case (R.id.settings_menu_option):

Intent j = new Intent(this, SettingsActivity.class);

startActivity(j);

return true;

default:

return super.onOptionsItemSelected(item);

}

}

[MENTION=439709]override[/MENTION]

public void onStart() {

super.onStart();

Log.d(TAG, "onStart() called");

}

[MENTION=439709]override[/MENTION]

public void onPause() {

super.onPause();

Log.d(TAG, "onPause() called");

}

[MENTION=439709]override[/MENTION]

public void onResume() {

super.onResume();

Log.d(TAG, "onResume() called");

}

[MENTION=439709]override[/MENTION]

public void onStop() {

super.onStop();

Log.d(TAG, "onStop() called");

SharedPreferences out_file = getPreferences(Context.MODE_PRIVATE);

SharedPreferences.Editor editor = out_file.edit();

editor.putInt(getString(R.string.total_outs_key), out_count);

editor.commit();

}

[MENTION=439709]override[/MENTION]

public void onDestroy() {

super.onDestroy();

Log.d(TAG, "onDestroy() called");

}

}

package edu.umkc.baldwin;

import android.app.Activity;

import android.app.FragmentManager;

import android.app.FragmentTransaction;

import android.os.Bundle;

public class SettingsActivity extends Activity {

[MENTION=439709]override[/MENTION]

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.settings_layout);

FragmentManager ttsFragmentManager = getFragmentManager();

FragmentTransaction ttsFragmentTransaction = ttsFragmentManager.beginTransaction();

EnableTTSPreferenceFragment ttsFragment = new EnableTTSPreferenceFragment();

ttsFragmentTransaction.add(R.id.tts_fragment, ttsFragment);

ttsFragmentTransaction.commit();

FragmentManager resetFragmentManager = getFragmentManager();

FragmentTransaction resetFragmentTransaction = resetFragmentManager.beginTransaction();

ResetFragment resetFragment = new ResetFragment();

resetFragmentTransaction.add(R.id.reset_fragment, resetFragment);

resetFragmentTransaction.commit();

}

}

package edu.umkc.baldwin;

import android.app.Fragment;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

public class ResetFragment extends Fragment {

[MENTION=439709]override[/MENTION]

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

View resetFragment = inflater.inflate(R.layout.reset_layout, container, false);

return resetFragment;

}

}

package edu.umkc.baldwin;

import android.os.Bundle;

import android.preference.PreferenceFragment;

public class EnableTTSPreferenceFragment extends PreferenceFragment {

[MENTION=439709]override[/MENTION]

public void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

addPreferencesFromResource(R.xml.tts_preference_fragment);

}

[MENTION=439709]override[/MENTION]

public void onSaveInstanceState(Bundle outState) {

super.onSaveInstanceState(outState);

}

}