Brent
Brent

Reputation: 778

saving data in an activity while moving to another activity

My problem is saving data in the main activity when I go to another activity. In practice program here i have 2 buttons in my main activity and 2 textviews when you click the first button you are brought into a second activity named firstapp with just a rating bar. I pick your rating then press the back button and in the textview under the button it gets the string value of the rating bar and tells the rating. However, when I click the second button and move to the 3rd activity named secondapp and do the rating, then go back to the main activity I see the proper information in the second textview but the first rating I did is now gone form the text view.

I dont know how to save that information in there and have been messing around with it for a while now

package com.example.brent.appmanagement;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    TextView firstrating, secondrating;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ratingapp1();
        ratingapp2();
        getrating();


    }



    private void getrating(){
        //firstrating = findViewById(R.id.savedrating1);
       // secondrating = findViewById(R.id.savedrating2);


        //Intent i = getIntent();
        //firstrating.setText(i.getExtras().getString("rating"));
       // secondrating.setText(i.getStringExtra("rating2"));

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String rating = extras.getString("rating");
            firstrating = findViewById(R.id.savedrating1);
            firstrating.setText(rating);
            String rating2 = extras.getString("rating2");
            secondrating = findViewById(R.id.savedrating2);
            secondrating.setText(rating2);
        }

    }
    private void ratingapp1(){
        Button rateapp1 = findViewById(R.id.rateapp1);
        rateapp1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT){
                    Toast.makeText(getApplicationContext(),
                            "Sorry you need to upgrade your phone before you 
can rate the apps", Toast.LENGTH_LONG).show();
                }else {
                    Intent intent = new Intent(getApplicationContext(), 
firstapp.class);
                    startActivity(intent);
                }
            }
        });
    }
    private void ratingapp2(){
        Button rateapp2 = findViewById(R.id.rateapp2);
        rateapp2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT){
                    Toast.makeText(getApplicationContext(),
                            "Sorry you need to upgrade your phone before you 
can rate the apps", Toast.LENGTH_LONG).show();
                }else {
//                    Bundle extras = getIntent().getExtras();
//                    if(extras != null) {
//                        firstrating =  findViewById(R.id.savedrating1);
//                        firstrating.setText(extras.getString("rating"));
//                    }
                    Intent intent2 = new Intent(getApplicationContext(), 
secondapp.class);
                    startActivity(intent2);
                }
            }
        });
    }
}

firstapp activity code

package com.example.brent.appmanagement;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.Toast;

public class firstapp extends AppCompatActivity {


    public RatingBar ratingBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_firstapp);
        ratingBar =  findViewById(R.id.ratingBar);
        return1();
    }
    public void rateMe(View view){

        Toast.makeText(getApplicationContext(),
                String.valueOf(ratingBar.getRating()), 
Toast.LENGTH_LONG).show();


    }
    private void return1(){
        Button returntomain1 = findViewById(R.id.returntomain1);
        returntomain1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(getApplicationContext(), 
MainActivity.class);

                i.putExtra("rating", "You have rated this app " + 
String.valueOf(ratingBar.getRating() + " stars out of 5"));
                startActivity(i);
            }
        });
    }
}

secondapp activity

package com.example.brent.appmanagement;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.Toast;

public class secondapp extends AppCompatActivity {

    public RatingBar ratingBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_secondapp);
        return2();
        ratingBar =  findViewById(R.id.ratingBar);
    }
    public void rateMe(View view){

        Toast.makeText(getApplicationContext(),
                String.valueOf(ratingBar.getRating()), 
Toast.LENGTH_LONG).show();
    }
    public void return2(){
        Button returntomain2 = findViewById(R.id.returntomain2);
        returntomain2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(getApplicationContext(), 
MainActivity.class);
                i.putExtra("rating2", "you have rated this app " + 
String.valueOf(ratingBar.getRating() + " stars out of 5"));
                startActivity(i);
            }
        });
    }
}

Dont mind the code that is commented out and if you need my xml just say so i just didnt want to post to much to look at and the submit buttons and the toast message is just so i could see it was pulling the right value. If there is something im doing that i shouldnt be please also tell me as i said im new to android this is just practice

thanks all

ok so i tried the startActivityForResult and set the result and override the onActivityresult method and i still have the same outcome even after i just called finish() instead of startActivity when going back to main activity...same thing happens i go into firstapp rate it go back to main and my data has transferred to main and is now in the test box....but then i go to second app rate it come back and the data from first app that was in the textbox is now gone

Upvotes: 0

Views: 1464

Answers (2)

Truong Giang Dam
Truong Giang Dam

Reputation: 500

The first I suggest you do not create new instance of MainActivity, just back to the MainActivity under your current activity(first app or second app) in the stack.

About handle the rating return:

  1. In first/second app activity, you should use SharedPreferences to save rating in first/second app activity. Here's a link to explain SharedPreferences.
  2. Start first/second app activity with startActivityForResult() in oder to get call back on onActivityResult() in your MainActivity.
  3. Every times user back from first/second app activity to MainActivity, onActivityResult() will be called, and here you get data rating from SharePreferences and update your TextView.

UPDATE try this

private void return1(){
    Button returntomain1 = findViewById(R.id.returntomain1);
    returntomain1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPref.edit();
            editor.putFloat("rating1", ratingBar.getRating());
            editor.commit();
            finish();
        }
    });
}

and

public void return2(){
    Button returntomain2 = findViewById(R.id.returntomain2);
    returntomain2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPref.edit();
            editor.putFloat("rating2", ratingBar.getRating());
            editor.commit();
            finish();
        }
    });
}

and in your MainActivity

@Override
protected void onResume() {
    super.onResume();
    updateRating();
}

private void updateRating(){
    SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
    float rating1 = sharedPref.getFloat("rating1",-1);
    float rating2 = sharedPref.getFloat("rating2",-1);
    // -1 is default value

    if(rating1 != -1) {
        firstrating.setText(String.valueOf(rating1));
    }

    if(rating2 != -1) {
        secondrating.setText(String.valueOf(rating2));
    }
}

Hope it help !

Upvotes: 0

cincy_anddeveloper
cincy_anddeveloper

Reputation: 1152

Your problem is that you are starting a new instance of MainActivity when you return thus, loosing all of your previous data. What you want to do is call startActivityForResult and inside of both firstapp and secondapp in the method return1 and return2() you should call setResult() passing in the RESULT_OK constant and an intent containing the data you want MainActivity to know about. Then call finish immediately after. Inside of MainActivity, you want to override the method onActivityResult() which will receive the data you passed to setResult().

Here's a link to the developer site that explains this API.

Upvotes: 1

Related Questions