Android xml(layout) used by multiple activity in project

495 views Asked by At

I am developing a project which has multiple activities and each activity sets values in Textview. Also I'm using same xml file containing multiple Textviews for all activities. I created multiple text view in one xml file. I don't know how to kept value of one activity in text view as new activity started it reload xml again so old value gone.

My activity runs sequentially and I want that if activity one set TCP Download(id) and other activity set TCP Upload(id), while TCP Upload setting value TCP Download value should intake not disappear

I want display value not vanished as new activity setContentView(R.layout.main) called it reload xml again and I want already set TextView value not disappear as intent pass to new activity.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:background="@color/black"
>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
<TextView
    android:id="@+id/tcpdownload1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dip"
    android:text="TCP Download: "
    android:textColor="#FFFFFF"
    android:textSize="16dip"
    android:layout_gravity="left"
 />

<TextView
    android:id="@+id/tcpdownload"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:textColor="#FFFFFF"
    android:textSize="16dip"
    android:layout_gravity="right" />  
</LinearLayout>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
<TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/tcpupload1"
    android:text="TCP Upload: "
    android:textColor="#FFFFFF" android:textSize="16dip"/>
<TextView  
    android:layout_width="120dp" 
    android:layout_height="wrap_content" 
    android:id="@+id/tcpupload"
    android:textColor="#FFFFFF" android:textSize="16dip"/>
</LinearLayout>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
<TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/tcprtt1"
    android:text="TCP RTT"
    android:layout_marginTop="10dip" android:textColor="#FFFFFF"
    android:textSize="16dip"/>

<TextView  
    android:layout_width="120dp" 
    android:layout_height="wrap_content" 
    android:id="@+id/tcprtt"
    android:layout_marginTop="10dip" 
    android:textColor="#FFFFFF" 
    android:textSize="16dip"/>
</LinearLayout>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView  
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:id="@+id/packetloss1"
        android:text="Packet Loss"
        android:layout_marginTop="10dip" 
        android:textColor="#FFFFFF" android:textSize="16dip"/>

    <TextView  
        android:layout_width="120dp" 
        android:layout_height="wrap_content" 
        android:id="@+id/packetloss"
        android:layout_marginTop="10dip" 
        android:textColor="#FFFFFF" android:textSize="16dip"/>
    </LinearLayout>
</LinearLayout>

activity A set value of id1 and now activity A --> activity B, in activity B i want to set value of id2 but i also want id1 value in display

3

There are 3 answers

5
Prachi On

You want to keep the TextView values while switching activities right?? You can save values in a sharedPref or simply hold values in a different class/model also you can pass values from one activity to other using intent for eg:

Intent mIntent = new Intent(contextInstance, Example.class);
mIntent.putExtra(key, value);
startActivity(mIntent);
2
hardwork On

You can use String buffer to store value of TextView,

like this.

StringBuffer s1 = new StringBuffer();
TextView tcpdownload = (TextView)findViewByid(R.id.tcpdownload1);
String str = "as you want to set as text";
s1.append(str);

and display s1 when its relative activity opened.

tcpdownload.setText(s1.tostring());

do this for all activity. Create different String-Buffer for different activity.

load them when new activity start respectively.

0
Android Killer On

There are few points i will mention:

  1. You can use the extra for intent when you are starting another activity to pass the value and get those value in destination activity and display in the textview. As shown below:

    Intent intent = new Intent(context, Destination.class);
    intent.putExtra(key, value);
    startActivity(intent);
    
  2. Or you can use the ShredPreference to store values. But it stores only premitive type and String. But it can avoid passing the same value(if you are using the same value) again and again.

  3. 3rd approach which is not recommended that you can go for storing in the database. This will allow to modify the existing data very well.