Question

Hello Everybody,

    I have an app that a couple of classmates and I did for a class project. We are now continuing the app to improve and streamline it. Here is what I have so far:

  • SplashScreen
  • HomePage
  • Hole 1 - 18
  • ScorePage
  • AboutPage

Home Screen Hole 1 - 18 Score Page About Page

I have pretty much got it all figured out except for a few small things. The issue that I am working on right now though is:

Passing data from each Hole Page to the Score Page.

I know how to pass it from page to page and I could brute force it, because that is how I initially had it, but it looks sloppy and I would like to not do that if possible.

//Code (partial)

//(From Hole 1)

@Override
public void onClick(View v)
{
    TextView tvScoreLbl = (TextView) findViewById(R.id.scoreLbl);
    tvScoreLbl.setText(String.valueOf(count));

    if(v == findViewById(R.id.btnAdd))
    {
        count++;
        tvScoreLbl.setText(String.valueOf(count));
    }
    else if(v == findViewById(R.id.btnMinus))
    {
        count--;
        tvScoreLbl.setText(String.valueOf(count));
    }
    else if(v == findViewById(R.id.btnPrev))
    {
        Intent i_prev = new Intent(Hole_01.this, HomePage.class);
        startActivity(i_prev);
    }
    else if(v == findViewById(R.id.btnNext))
    {
        Intent i_pass = new Intent(Hole_01.this, ScorePage.class);
        i_pass.putExtra("score1", tvScoreLbl.getText().toString());
        Intent i_next = new Intent(Hole_01.this, Hole_02.class);
        startActivity(i_next);
    }


//(From ScorePage)

String score;
TextView tvScore1 = (TextView) findViewById(R.id.tvScore1);

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.scorepage);

    Button btnHome = (Button) findViewById(R.id.btnHome);
    btnHome.setOnClickListener(this);

    score = getIntent().getExtras().getString("score1");
    tvScore1.setText(score);
}

Thanks in advance.

Was it helpful?

Solution

The best answer depends on how long you need your data to persist.

If you need it to persist across a long period of time (days/weeks) then store the data to the internal or external storage using either SQLite or creating a custom object that represents all the data from the 'hole page' and serialize that object and write it to disk.

If you don't need the data to persist then I'd do as you currently are and continue passing the data as you are, from Activity to Activity. It may seem 'sloppy' to you but it is how you are supposed to pass data between Activities.

Perhaps creating an object for the data you want to pass and have it implement the Parcelable interface would make it less 'sloppy' in your eyes. Here's a link to a tutorial for Parcelable objects.

Another option is to use the global Application context as suggested already BUT be warned it comes with a big problem - if the app is killed in the background any data stored in the Application class is lost, so you'll have to account for that happening.

A DB is excessive if you're only storing a tiny bit of info, the Application context will lose data if the app is killed by the OS.

So if you want to persist data for a period of time, use serialization to write the object ( if it's just the score then a simple File IO stuff would do the job with no need for serialization). Or if you only need it to persist while the app is running (foreground or background) then pass the data between Activities.

OTHER TIPS

Rather than passing data between all your Intents with putExtra why not use a little SQLite DB to store the result of each activity that the score screen could read.

SQLite Docs

Android Notepad Tutorial is good for SQLite example as well

Hold all the state in an Application Class.

Write to it as needed, from whatever Activity.

Read from it in the ScorePage Activity.

More details and example available here: (Using the Android Application class to persist data)

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