Akash Preet Akash Preet - 5 months ago 23
Android Question

How can I use the same Activity for different content in Android?

Consider I am using the linear layout for my app


<TextView
android:text="product 1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textColor="@android:color/white"
android:textSize="54sp"
android:background="#009611" />

<TextView
android:text="product 2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textColor="@android:color/white"
android:textSize="54sp"
android:background="#123431" />

<TextView
android:text="product 3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textColor="@android:color/white"
android:textSize="54sp"
android:background="#009688" />
</LinearLayout>


My output is:

akash preet sample output

when I select product1 it should open product1_des Activity

when I select product2 it should open product2_des Activity

when I select product3 it should open product3_des Activity

and goes on..

And I have 1000 product should I create 1000 Activity?
How can I use the product_des Activity (i.e single Activity) to support all my 1000 product with different description that is related to that product?
Please help me.

Answer

And I have 1000 product should I create 1000 Activity? How can I use the product_des Activity (i.e single Activity) to support all my 1000 product with different description that is related to that product? Please help me.

NO!!!

You can create an Activity called for example ProductDetail but only one, and then everywhere you want to show a product detail you can use putExtras()

Intent intent = new Intent(this, ProductDetail.class);
intent.putExtra("productName", ProductName);
startActivity(intent);

And in ProductDetail you can get the name of the product with a Bundle

Bundle b = new Bundle();
b = getIntent().getExtras();
String name = b.getString("productName");
Comments