-1

I have an activity called "PurcahseActivity" and the value of user's transactions is displayed there. "TOTAL: € 1138.50" in the attached picture. The value is generated after a user has made a purchase. enter image description here

I have another activity called "SalesReportActivity" and here, I need to display the same value in this layout.

enter image description here

What is the best way to achieve this?

This is my PurchaseLayout.xml. 'net_total' is the ID of the value displayed.

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="64dp"
    android:layout_gravity="center_horizontal" >

    <LinearLayout
        android:id="@+id/net_total_value"
        android:layout_width="85dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="5dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/net_total"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:textColor="@color/dark_grey_standard_text"
            android:textSize="15sp"
            android:textStyle="bold" />
    </LinearLayout>

</TableRow>

I have researched a lot but couldn't find any solution for this specific question. Basically, i just want to display the same value in my salesReport.

Malith Lakshan
  • 762
  • 6
  • 12
Asb
  • 49
  • 4

3 Answers3

0

The easiest way to do this:

  1. In PurcahseActivity, after a user has made a purchase, save this value to sharedPreferences.

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    sharedPreferences.edit().putFloat("total", value).apply();
    
  2. In SalesReportActivity load this value:

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    float value = sharedPreferences.getFloat("total", 0f);
    
Stanislaw Baranski
  • 1,308
  • 9
  • 18
0

There are 3 ways that I know of, which can help you:

  • If you're starting the second activity after the first one, you can use Intent.putExtra and retrieve that value in the second activity
  • If that value is going to be reused in more than 1 place, it would make sense to actually save it in SharedPreferences and then fetch it wherever needed
  • If you want to asynchronously get and send that value between any two components but you have no desire of saving it, you can use a great library called EventBus

I had not given you any code, since each of these 3 methods is well-known and many times explained. You can always find out on your own how to use SharedPreferences and Intents. As far as the EventBus goes, it has great documentation, but if you still had any trouble using it, feel free to ping me or write a question here.

Vucko
  • 7,371
  • 2
  • 27
  • 45
0

The normal way of passing information through activity is using Intent when starting the activity. In the source activity,

Intent intent = new Intent(this, TargetActivity.class);
intent.putExtra("YOUR_FLAG", yourValue);
startActivity(intent);

And then in your TargetActivity, e.g. in the onCreate(...)

float yourValue;
Bundle extras = getIntent().getExtras();
if (extras != null) {
    yourValue = extras.getFloat("YOUR_FLAG");
}

To persist your data across more than 2 activities, then use SharedPreferences. There are other approaches like Database, File, or even EventBus, if a more complex solution is required.

Elye
  • 53,639
  • 54
  • 212
  • 474