dyziok122 dyziok122 - 4 months ago 23
Android Question

What's wrong? Android editText Activity1 to Activity2

I have such a code

public class Activity1 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity1);
EditText editText = (EditText)findViewById(R.id.eText);
}
public void onClick11(View view) {
EditText editText = (EditText)findViewById(R.id.eText);
String w = editText.getText().toString();
Intent intent = new Intent(this, Activity2.class);
intent.putExtra("value", w);
startActivity(new Intent(getApplicationContext(), Activity2.class));
}}


Activity2

public class Activity2 extends AppCompatActivity {
public int o;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
setContentView(R.layout.activity2);
String w = intent.getStringExtra("value");
o = Integer.parseInt(w);
.....
}
.....}


activity1.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.karol.MyProgramm.Activity1">

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">

<TextView
android:layout_width="166dp"
android:layout_height="184dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Bla bla bla"
android:gravity="center"
android:id="@+id/textView2"
android:layout_gravity="center_vertical" />
<EditText android:layout_gravity="center_vertical"
android:layout_width="82dp"
android:text="0"
android:gravity="center_horizontal"
android:id="@+id/eText" />

<Button
android:layout_width="wrap_content"
android:layout_height="78dp"
android:text="OK"
android:inputType="number"
android:onClick="onClick11"
android:id="@+id/button7"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_weight="0.71" />

</TableRow>
</RelativeLayout>


Activity2 don't include.... and process back to other activity...
I want give value to Activity2.

Answer

Change startActivity(new Intent(getApplicationContext(), Activity2.class));

To startActivity(intent);

On onClick11, because this way you are overriding your putExtra.

Comments