Leschu
Leschu

Reputation: 13

Starting previous activity with Intent, without creating a new one

I want to have my MainActivity which shows a list of different TextViews. The second Activity contains an EditText field. When I hit 'Submit' in the second Activity, I want to add the String from EditText to the list of TextViews in MainActivity.

The problem I have is that there is a new MainActivity started each time, so that only the latest Text is shown. I want to go back to my MainActivity and kind of "collect" the texts in there. Here is some code:

The MainActivity:

public class FullTimeline extends AppCompatActivity {

    private LinearLayout linLayout;
    private FloatingActionButton addEntry;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_full_timeline);
        linLayout = (LinearLayout) findViewById(R.id.entriesLinear);
        addEntry = (FloatingActionButton) findViewById(R.id.floatingActionButton); //starts the second Activity with the EditText

        //Here I want to get the text from my second Activity
        Intent intent = getIntent();
        String entry = intent.getStringExtra(EntryTextActivity.EXTRA_ENTRY);
        linLayout.addView(createNewTextView(entry), 0);
    }

    public void openEntryActivity(View view){
        Intent intent = new Intent(this, EntryTextActivity.class);
        startActivity(intent);

    }

    private TextView createNewTextView(String text) {
        final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        final TextView textView = new TextView(this);
        textView.setLayoutParams(lparams);
        textView.setText(text);
        return textView;
    }
}

This is the second Activity with the EditText:

public class EntryTextActivity extends AppCompatActivity {

public static final String EXTRA_ENTRY = "com.xyz.ENTRY";
    private Button submitButton;
    private EditText editTextEntry;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_entry_text);
        submitButton = (Button) findViewById(R.id.submitButton);
        submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(EntryTextActivity.this, FullTimeline.class);
                editTextEntry = (EditText) findViewById(R.id.editTextEntry);
                String entryText = editTextEntry.getText().toString();
                intent.putExtra(EXTRA_ENTRY, entryText);
                startActivity(intent);//this starts a new instance of the MainActivity ?
            }
        });
    }
}

I hope that helps understanding my problem. I could not find any working solutions on here.

Upvotes: 1

Views: 313

Answers (2)

Shift Kun
Shift Kun

Reputation: 26

You need to use startActivityForResult()

Here's a Nishant's simple explanation on how to work with it: - https://stackoverflow.com/a/10407371/5648172

Basically, you want to start your EntryTextActivity with startActivityForResult() instead of startActivity().

Upvotes: 1

seba
seba

Reputation: 153

I haven't tested it but i think that in the case you don't want to use a database, you can set your MainActivity as "singleInstance" in your manifest :

<activity android:name="{your }" android:launchMode= "singleInstance" />

Then you can add the new text in an your MainActivity's onResume

 @Override
  public void onResume() {
    super.onResume();
    Intent intent = getIntent();
    String entry = intent.getStringExtra(EntryTextActivity.EXTRA_ENTRY);
    linLayout.addView(createNewTextView(entry), 0);
  }

Upvotes: 0

Related Questions