0

have created a main activity with an editText and by clicking the button a TextView is created in the second activity and the text is passed to it. Now I've to go back to the 1st activity and give some new data so that the new data is also displayed along with the old data. But in my case instead of adding, a new page (with the new data) is displayed overlapping the old data. As a beginner i tried my level best but i cant find any solutions. Need some help...

Here's my main activity

public class MainActivity extends Activity 
{

EditText txt1;
String value1;
Button button_1;
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txt1=(EditText)findViewById(R.id.editText1);
    value1=txt1.getText().toString();

    button_1=(Button)findViewById(R.id.button1);
    button_1.setOnClickListener(onClick());


}

private OnClickListener onClick() 
{
    // TODO Auto-generated method stub
    return new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
            Intent i=new Intent(getApplicationContext(),Activity2.class);
            i.putExtra("v",txt1.getText().toString());
            startActivity(i);

        }
    };
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) 
    {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

My second Activity

public class Activity2 extends MainActivity
{
LinearLayout ll;
TextView txtview;
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity2);

    ll=(LinearLayout)findViewById(R.id.linear);

    txtview=new TextView(this);
    txtview.setText(getIntent().getExtras().getString("v"));



    //txtview.setText(String.valueOf(getIntent().getExtras().getString("1v")));
    ll.addView(createNewTextView(txtview.getText().toString()));

    Button exit=(Button)findViewById(R.id.button1);
    exit.setOnClickListener(new Button.OnClickListener()
    {

        @Override
        public void onClick(View v) 
        {
            // TODO Auto-generated method stub
            finish();
        }
    });
    Button New=(Button)findViewById(R.id.button2);
    New.setOnClickListener(new Button.OnClickListener()
    {

        @Override
        public void onClick(View v) 
        {
            // TODO Auto-generated method stub

            Intent i=new Intent(getApplicationContext(),MainActivity.class);
            startActivity(i);
        }
    });
}
private View createNewTextView(String text) 
{
    // TODO Auto-generated method stub
    final LayoutParams lp=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    final TextView txtview=new TextView(this);
    txtview.setLayoutParams(lp);
    txtview.setText("New text: "+text);
    return txtview;
}
}

activity_main.xml file

<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.self.MainActivity" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:ems="10"
    android:inputType="text"
    android:text="@string/e" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText1"
    android:layout_alignRight="@+id/editText1"
    android:layout_below="@+id/editText1"
    android:text="@string/app_name" />

activity2.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/linear"
    android:layout_width="match_parent"
    android:layout_height="418dp"
    android:orientation="vertical" >

</LinearLayout>

<LinearLayout
    android:id="@+id/hl"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button2"
        android:layout_width="154dp"
        android:layout_height="wrap_content"
        android:text="@string/New" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/exit" />

</LinearLayout>

AndroidManifest.xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.studentmarklist"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="16" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".act2"></activity>
</application>

Subi
  • 23
  • 7
  • You mean.. First you enter "data1" in Activity 1, go to Activity 2. It should show "data1". You press back. Enter "data 2" in Activity 2, go to Activity 2, now it *should* show "data 1 data 2"? – MysticMagicϡ Sep 02 '14 at 08:23
  • First I enter "data1" in Activity 1, go to Activity 2. It should show "data1". Press back. Enter "data 2" in Activity 1, go to Activity 2, now it should show "data1 and data2". – Subi Sep 02 '14 at 08:27
  • That's hard as your activity will be created every time. You can do one thing. Put the comma separated strings in Shared Preference each time. And show in textviews each time in loop. – MysticMagicϡ Sep 02 '14 at 10:42

2 Answers2

0

You should look a at the life-cycle of activities in android. Most of the time the activity is not destroyed and used again. Too force this override the onbackpressed function like

@Override
public void OnBackPressed(){
super.OnBackPressed();
finisch();
}

The Finisch link stackoverflow

Community
  • 1
  • 1
marcel
  • 313
  • 4
  • 10
0

If you want to preserve old data each time when activity starts, you can use Shared Preferences in activity 2.

Save the data in shared preference while you setText and retrieve it in onCreate to show next time.

See below code snippet:

SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity2);

    sp = getSharedPreferences("textPref", Context.MODE_PRIVATE);
    if(sp.contains("text"))
        newText = sp.getString("text", "").concat(" and ").concat(getIntent().getExtras().getString("v"));
    else
        newText = getIntent().getExtras().getString("v");


    ll=(LinearLayout)findViewById(R.id.ll);
    ll.addView(createNewTextView(newText));
}

private View createNewTextView(String text) 
{
    // TODO Auto-generated method stub
    final LayoutParams lp=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    final TextView txtview=new TextView(this);
    txtview.setLayoutParams(lp);
    txtview.setText("New text: "+text);

    Editor editor = sp.edit();
    editor.putString("text", text);
    editor.commit();

    return txtview;
}

Hope this helps.

EDIT as per OP's clarification:

To show new TextView for each time, try storing each value in Shared Preference with comma separation.

Split them in array. So you can get the array of Texts to be shown. Then show them in new TextView in a loop, and add TextView in LinearLayout.

Snippet:

SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity2);

    sp = getSharedPreferences("textPref", Context.MODE_PRIVATE);
    if(sp.contains("text"))
        newText = sp.getString("text", "").concat(" and ").concat(getIntent().getExtras().getString("v"));
    else
        newText = getIntent().getExtras().getString("v");


    ll=(LinearLayout)findViewById(R.id.ll);
    createNewTextView(newText);
}

private void createNewTextView(String text) 
{
    // TODO Auto-generated method stub
    Editor editor = sp.edit();
    editor.putString("text", text);
    editor.commit();

    String[] texts = text.split(",");

    for (int i =0; i < texts.length; i++){
        final LayoutParams lp=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        final TextView txtview=new TextView(this);
        txtview.setLayoutParams(lp);
        txtview.setText("New text: "+texts[i]);
        ll.addView(txtview);
    }
}

Hope this is clear.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • Your code helps me to concat the values. Thank You. But I want these answers to be displayed in seperate textviews . i.e "data1" in seperate text view and "data2" in another seperate text view – Subi Sep 02 '14 at 09:37
  • @Subi Then you will have to create new TextView each time. :) – MysticMagicϡ Sep 02 '14 at 09:38
  • I tried but as a beginner its complicated for me . Could u give me sample about it???? – Subi Sep 02 '14 at 10:13
  • @Subi Please tell me what exactly you want to do? New textview should be created each time you go to Activity 2? – MysticMagicϡ Sep 02 '14 at 10:16
  • yes. when i enter a "data1" and click the button it should create a textview in the second activity with the "data1" inside the textview. now i'll go back to activity 1 and enter the new "data2" and when i click the button the next activity should display the 2 textviews with "data1" inside one textview and "data2" inside second textview . – Subi Sep 02 '14 at 10:25
  • Works great Thanks.... It would be great if each value is displayed in seperate textview without using 'Comma'. Just inform me if u find the exact solution in future. Thanks... – Subi Sep 02 '14 at 11:34
  • @Subi surely. happy coding. accept the answer if your issue is resolved :) – MysticMagicϡ Sep 02 '14 at 11:39