Reputation:
I have an Android app, which about writing notes. When I press the Edit button in Second2 Activity it will move to the Second3 Activity without any problems (In Second3 Activity I can edit the note without any problem). But when I press the Save button, it will return to Second2 but nothing appears there! It's just an empty Activity! Help me, please!
MainActivity class:
package com.twitter.i_droidi.mynotes;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.List;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
public class MainActivity extends ActionBarActivity implements AdapterView.OnItemClickListener {
ListView lv;
NotesDataSource nDS;
List<NotesModel> notesList;
String[] notes;
int i;
ArrayAdapter<String> adapter;
AdView mAdView;
AdRequest adRequest;
public static long back_pressed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nDS = new NotesDataSource(this);
lv = (ListView) findViewById(R.id.lv);
nDS.open();
notesList = nDS.getAllNotes();
nDS.close();
notes = new String[notesList.size()];
for (i = 0; i < notesList.size(); i++) {
notes[i] = notesList.get(i).getTitle();
}
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,
android.R.id.text1, notes);
lv.setAdapter(adapter);
registerForContextMenu(lv);
lv.setOnItemClickListener(this);
mAdView = (AdView) findViewById(R.id.adView);
adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
@Override
public void onBackPressed()
{
if (back_pressed + 2000 > System.currentTimeMillis()) super.onBackPressed();
else Toast.makeText(this, "Press once again to exit!", Toast.LENGTH_SHORT).show();
back_pressed = System.currentTimeMillis();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent nView = new Intent(this, Second2.class);
nView.putExtra("id", notesList.get(position).getId());
nView.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(nView);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_delete, menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.delete:
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
nDS.open();
nDS.deleteNote(notesList.get(info.position).getId());
notesList = nDS.getAllNotes();
nDS.close();
notes = new String[notesList.size()];
for (i = 0; i < notesList.size(); i++) {
notes[i] = notesList.get(i).getTitle();
}
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,
android.R.id.text1, notes);
lv.setAdapter(adapter);
registerForContextMenu(lv);
lv.setOnItemClickListener(this);
Toast nDelete = Toast.makeText(this, R.string.deleted, Toast.LENGTH_LONG);
nDelete.show();
}
return super.onContextItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mainMenuNewNote:
Intent nNote = new Intent(this, Second.class);
startActivity(nNote);
return true;
case R.id.mainMenuAbout:
AlertDialog.Builder aboutDialog = new AlertDialog.Builder(this);
aboutDialog.setTitle(getString(R.string.about_title));
aboutDialog.setMessage(R.string.about_body);
aboutDialog.setIcon(R.drawable.my_notes);
aboutDialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface aboutDialog, int witch) {
// Do Not Do Anything.
}
});
aboutDialog.show();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Second class:
package com.twitter.i_droidi.mynotes;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;
public class Second extends ActionBarActivity {
NotesDataSource nDS;
EditText noteTitle;
EditText noteBody;
int id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent in = getIntent();
id = in.getIntExtra("id", 0);
noteTitle = (EditText) findViewById(R.id.note_title);
noteBody = (EditText) findViewById(R.id.note);
nDS = new NotesDataSource(this);
nDS.open();
NotesModel note = nDS.getNote(id);
nDS.close();
noteTitle.setText(note.getTitle());
noteBody.setText(note.getBody());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_second, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.secondMenuSave:
if (!noteTitle.getText().toString().isEmpty() && !noteBody.getText().toString().isEmpty()) {
nDS.open();
nDS.createNote(noteTitle.getText().toString(), noteBody.getText().toString());
nDS.close();
Toast nSave = Toast.makeText(this, R.string.saved, Toast.LENGTH_LONG);
nSave.show();
Intent nView = new Intent(this, MainActivity.class);
nView.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(nView);
} else {
Toast notSave = Toast.makeText(this, R.string.do_not_save, Toast.LENGTH_LONG);
notSave.show();
}
return true;
}
return super.onOptionsItemSelected(item);
}
}
Second2 class:
package com.twitter.i_droidi.mynotes;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
public class Second2 extends ActionBarActivity {
TextView viewNoteTitle;
TextView viewNoteBody;
int id;
NotesDataSource nDS;
NotesModel note;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second2);
Intent in = getIntent();
id = in.getIntExtra("id", 0);
viewNoteTitle = (TextView) findViewById(R.id.view_note_title);
viewNoteBody = (TextView) findViewById(R.id.view_note);
nDS = new NotesDataSource(this);
nDS.open();
note = nDS.getNote(id);
nDS.close();
viewNoteTitle.setText(note.getTitle());
viewNoteBody.setText(note.getBody());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_second2, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.second2MenuEdit:
Intent nEdit = new Intent(this, Second3.class);
nEdit.putExtra("id", note.getId());
nEdit.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(nEdit);
}
return super.onOptionsItemSelected(item);
}
}
Second3 class:
package com.twitter.i_droidi.mynotes;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;
public class Second3 extends ActionBarActivity {
NotesDataSource nDS;
EditText noteTitle;
EditText noteBody;
int id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent in = getIntent();
id = in.getIntExtra("id", 0);
noteTitle = (EditText) findViewById(R.id.note_title);
noteBody = (EditText) findViewById(R.id.note);
nDS = new NotesDataSource(this);
nDS.open();
NotesModel note = nDS.getNote(id);
nDS.close();
noteTitle.setText(note.getTitle());
noteBody.setText(note.getBody());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_second, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.secondMenuSave:
if (!noteTitle.getText().toString().isEmpty() && !noteBody.getText().toString().isEmpty()) {
nDS.open();
nDS.updateNote(id, noteTitle.getText().toString(), noteBody.getText().toString());
nDS.close();
Toast nSave = Toast.makeText(this, R.string.saved, Toast.LENGTH_LONG);
nSave.show();
Intent nView = new Intent(this, Second2.class);
nView.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(nView);
} else {
Toast notSave = Toast.makeText(this, R.string.do_not_save, Toast.LENGTH_LONG);
notSave.show();
}
return true;
}
return super.onOptionsItemSelected(item);
}
}
Upvotes: 2
Views: 57
Reputation: 8519
The Second Activity gets data from Intent from these two lines:
Intent in = getIntent();
id = in.getIntExtra("id", 0);
So when you go back to this Activity from Third Activity that is when you save your note in the following lines:
Intent nView = new Intent(this, Second2.class);
nView.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(nView);
You have to add the id into the Intent as you did in the Main Activity class, you might do it as below, but you dont have the noteList
array in the third activity.
nView.putExtra("id", notesList.get(position).getId());
So the easiest way to get the data in the activity is to call
finish();
This is because the Second Activity is still in the Activity Stack and you dont have to start it again, you can just finish the Third Activity and the Second will just show up because its the last Activity in the stack. But the title wont be updated so you'll have to start the Activity from beginning like the code below:
When you are done saving the note.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.secondMenuSave:
if (!noteTitle.getText().toString().isEmpty() && !noteBody.getText().toString().isEmpty()) {
nDS.open();
nDS.updateNote(id, noteTitle.getText().toString(), noteBody.getText().toString());
nDS.close();
Toast nSave = Toast.makeText(this, R.string.saved, Toast.LENGTH_LONG);
nSave.show();
Intent nView = new Intent(this, Second2.class);
nView.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
nView.putExtra("id", id);
startActivity(nView);
finish();
} else {
Toast notSave = Toast.makeText(this, R.string.do_not_save, Toast.LENGTH_LONG);
notSave.show();
}
return true;
}
return super.onOptionsItemSelected(item);
}
For you hopefully last problem, In the Second Activity override the onBackPressed method and add the code below:
@Override
public void onBackPressed()
{
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
Upvotes: 1