N
N
nikita_chiru2016-05-07 19:43:12
Android
nikita_chiru, 2016-05-07 19:43:12

Transferring data between activities.?

How to transfer data between activities?
how to use this data in 2 activities?
and how to do so that each new data would create a string for itself and throw it there?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
E
Elysey, 2016-05-07
@Elysey

Before calling startActivity, pass data to Intent.
For example:

String data = "Эти данные необходимо передать";

Intent i = new Intent(MainActivity.this, SecondActitviy.class);
i.putExtra("testNameData", data);
startActivity(i);

And in the second activity, the getIntent method is called and the data is pulled out by the specified key.
For example:
In this way, various types of data can be transferred

R
Rou1997, 2016-05-07
@Rou1997

When switching between Activities, Intent is used, it has a setData method (and, it seems, getData), through them they put / take data. Google, and you will find not only the code, but also tutorials, it seems that Klimov had one.

P
Peter, 2016-05-07
@petermzg

Seems like this:
In AndroidManifest.xml

<application
        android:allowBackup="true"
        android:icon="@drawable/emblem"
        android:label="@string/app_name" android:name="AppEngine">
        ...
</application>

Where will you store the code?
public class AppEngine extends Application {
  @Override
  public void onCreate(){
    super.onCreate();
  }
}

From the activity you will have access to the instance of this class through (AppEngine)getApplication();

M
Max Pleshkov, 2016-05-07
@zzWerOk

To transfer data from an array, for example, you can do this:

Intent NewActivityIntent;
NewActivityIntent = new Intent(MainActivity.this, NewActivity.class);

int items_count = 0;
for (int i = 0; i < item_List.size(); i++) {

     String item_name = "item_" + items_count;
     NewActivityIntent.putExtra(item_name, "Данные из массива под номером " + i);
     items_count++;

}

NewActivityIntent.putExtra("items_count", items_count);

In the new activity, you can get them like this:
Bundle extras = getIntent().getExtras();

int items_count = extras.getInt("items_count");

for (int i = 0; i < items_count; i++) {
     String string = "item_" + i;
     String string_item = extras.getStringArray(string);
     item_List.add(string_item );
}

You can also pass an array of strings by inserting it into the parameter .putExtra("key",String[]) and extracting it by calling the extras.getStringArray("key");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question