Android – Bundle and putExtra

Bundle  este o clasă publică, finală, care permite accesarea de variabile ce au fost asociate ca date extinse activităţii anterioare. Wow…Simplu, nu?! Eu aş spune că NU. 🙂

Această clasă este de fapt un fel de “porumbel mesager”, mai precis, prin intermediul unei metode, putExtra, aparținând de această dată clasei Intent, se pot trimite de la o activitate la alta diverse valori pe baza principiului cheie-valoare. Aceste valori sunt captate în interiorul activității ce a fost lansată cu ajutorul clasei Bundle. Se realizază o instanță a acesteia,  this.getIntent().getExtras(); , după care se obțin valorile trimise în funcție de tipul de date asociat.

Exemple:

String:

Intent intent=new Intent(this, NextActivity.class);
intent.putExtra("paramString","unString");
startActivity(intent);

if (NextActivity.this.getIntent().getExtras() != null)
{
   Bundle bundle = this.getIntent().getExtras();
   String param1=bundle.getString("paramString");
}

Integer:

Intent intent=new Intent(this, NextActivity.class);
intent.putExtra("paramInt",21);
startActivity(intent);

if (NextActivity.this.getIntent().getExtras() != null)
{
    Bundle bundle = this.getIntent().getExtras();
    int param1=bundle.getInt("paramInt");
}

Aplicație demonstrativă:

Pentru a exemplifica aceste noțiuni am realizat o mini-maxi-aplicațiune alcătuită din două activități . Scopul este de a trimite mesaje din prima activitate către a doua.

Design:

-primul ecran

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:background="@drawable/white">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mesaj 1: "
android:layout_marginBottom="10dip"
android:textColor="@drawable/orange"
android:textStyle="bold">
</TextView>
<EditText android:id="@+id/eTxtParam1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dip">
</EditText>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mesaj 2: "
android:layout_marginBottom="10dip"
android:textColor="@drawable/green"
android:textStyle="bold">
</TextView>
<EditText android:id="@+id/eTxtParam2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="40dip">
</EditText>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical">
<Button
android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnTrimite_onClick"
android:background="@drawable/style_send_button"
android:layout_marginBottom="30dip">
</Button>
</LinearLayout>
</LinearLayout>

-al doilea ecran

next_screen.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:background="@drawable/white">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mesaj 1: "
android:layout_marginBottom="10dip"
android:textColor="@drawable/orange"
android:textStyle="bold">
</TextView>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/txtParam1"
android:background="@drawable/gray"
android:padding="5dip"
android:layout_marginBottom="30dip"
android:textStyle="italic"
android:textColor="@drawable/black">
</TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mesaj 2: "
android:layout_marginBottom="10dip"
android:textColor="@drawable/green"
android:textStyle="bold">
</TextView>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/txtParam2"
android:background="@drawable/gray"
android:textStyle="italic"
android:padding="5dip"
android:layout_marginBottom="40dip"
android:textColor="@drawable/black">
</TextView>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/open_msg">
</ImageView>
</LinearLayout>
</LinearLayout>

-style buton

style_send_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false"
android:drawable="@drawable/green_msg" />
<item android:state_pressed="true"
android:drawable="@drawable/red_msg" />
</selector>

Codul Java:

-prima activitate

BundleProjectActivity.java


package org.app.bundle;

import android.app.Activity;

import android.app.AlertDialog;

import android.content.DialogInterface;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.EditText;

public class BundleProjectActivity extends Activity

{

EditText eTxtParam1, eTxtParam2;

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

eTxtParam1 = (EditText)findViewById(R.id.eTxtParam1);

eTxtParam2 = (EditText)findViewById(R.id.eTxtParam2);

}

public void onResume()

{

super.onResume();

eTxtParam1.setText("");

eTxtParam2.setText("");

}

public void btnTrimite_onClick(View v)

{

if(eTxtParam1.getText().length() > 0 && eTxtParam2.getText().length() > 0)

{

Intent intent=new Intent(this, NextActivity.class);

intent.putExtra("param1", eTxtParam1.getText()+"");

intent.putExtra("param2", eTxtParam2.getText()+"");

startActivity(intent);

}

else

{

AlertDialog alertDialog = new AlertDialog.Builder(BundleProjectActivity.this).create();

alertDialog.setTitle("Alerta");

alertDialog.setIcon(getResources().getDrawable(R.drawable.contact));

alertDialog.setMessage("Completati toate campurile!");

alertDialog.setButton("OK", new DialogInterface.OnClickListener()

{

public void onClick(DialogInterface dialog, int which)

{

}

});

alertDialog.show();

}

}

}

– a doua activitate

NextActivity.java


package org.app.bundle;

import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;

public class NextActivity extends Activity

{

String param1, param2;

TextView txtParam1, txtParam2;

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.next_screen);

txtParam1 = (TextView)findViewById(R.id.txtParam1);

txtParam2 = (TextView)findViewById(R.id.txtParam2);

if (NextActivity.this.getIntent().getExtras() != null)

{

Bundle bundle = this.getIntent().getExtras();

param1=bundle.getString("param1");

param2=bundle.getString("param2");

}

if(param1!=null && param2!=null)

{

txtParam1.setText(param1);

txtParam2.setText(param2);

}

}

}

Advertisements
Android – Bundle and putExtra

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s