New to android, so I don't really know what's going on here;
Right now, on my main activity, I have a dropdown spinner; clicking a button goes to a second activity where there's a listview with information from the contents of the dropdown spinner.
When I click another button and go to a third activity, then press back, the information from the contents of the dropdown spinner is no longer there; there are null values instead.
Anybody know what could be the problem? Does the back button send an intent?
second activity:
public class classList extends AppCompatActivity {
ListView listView;
SimpleCursorAdapter mAdapter;
static private String[] classes = {"class1","class2"};
String course;
String number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
course = intent.getStringExtra("com.naomi.classAlert.course");
number = intent.getStringExtra("com.naomi.classAlert.number");
Toast.makeText(getApplicationContext(), course+number,
Toast.LENGTH_LONG).show();
setContentView(R.layout.activity_class_list);
listView = (ListView) findViewById(R.id.list);
String[] values = new String[] {
course+number+" 001", course+number+" 002", course+number+" 003"
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_2, android.R.id.text1, values);
listView.setAdapter(adapter);
// ListView Item Click Listener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(),
"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
.show();
Intent intent1 = new Intent(classList.this, pickSection.class);
intent1.putExtra("com.naomi.classAlert.classList.course",course);
intent1.putExtra("com.naomi.classAlert.classList.number", number);
intent1.putExtra("com.naomi.classAlert.classList.section",position+1);
startActivity(intent1);
}
});
}
}
Third activity:
public class pickSection extends AppCompatActivity {
String course;
String number;
int section;
//SharedPreferences faves = PreferenceManager
// .getDefaultSharedPreferences(this);
SharedPreferences faves;
SharedPreferences.Editor editor;
Button button;
TextView courseInfo;
TextView capacity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
faves = getSharedPreferences("preferencename",0);
editor = faves.edit();
setContentView(R.layout.activity_pick_section);
button = (Button) findViewById(R.id.favouriteButton);
courseInfo = (TextView) findViewById(R.id.courseinfo);
capacity = (TextView) findViewById(R.id.capacity);
Intent intent2 = getIntent();
course = intent2.getStringExtra("com.naomi.classAlert.classList.course");
number = intent2.getStringExtra("com.naomi.classAlert.classList.number");
section = intent2.getIntExtra("com.naomi.classAlert.classList.section",0);
courseInfo.setText(course+number+" "+section);
capacity.setText("dummy text");
String exists = faves.getString(course+number+section,"not found");
if (!exists.equals("not found")){
button.setText("Remove from favourites");
}
}
public void addToFavourites(View v) {
String exists = faves.getString(course+number+section,"not found");
if (!exists.equals("not found")) { //it exists in favourites
editor.remove(course + number + section);
button.setText("Add to favourites");
} else {
editor.putString(course+number+section, "favourite");
button.setText("Remove from favourites");
}
editor.commit();
}
}
An activity can be closed via the back button on the phone. In this case the finish() method is performed. If the activity was started with the startActivity(Intent) method call, the caller requires no result or feedback from the activity which now is closed.
If you start the activity with the startActivityForResult() method call, you expect feedback from the sub-activity. Once the sub-activity ends, the onActivityResult() method on the sub-activity is called and you can perform actions based on the result.
public void onItemClick (....){
// start pickSection
Intent intent1 = new Intent(classList.this, pickSection.class);
intent1.putExtra("com.naomi.classAlert.classList.course",course);
intent1.putExtra("com.naomi.classAlert.classList.number", number);
intent1.putExtra("com.naomi.classAlert.classList.section",position+1);
startActivity(intent1);
startActivityForResult(intent1, 0);
}
When back button pressed, pickSection is finished, it can send data back to its caller via an Intent. This is done in the finish() method. @Override
public void finish() {
Intent intent1 = new Intent();
intent1.putExtra("com.naomi.classAlert.classList.course",course);
intent1.putExtra("com.naomi.classAlert.classList.number", number);
intent1.putExtra("com.naomi.classAlert.classList.section",position+1);
// Activity finished ok, return the data
setResult(RESULT_OK, intent1);
super.finish();
}
Once the pickSection finishes, the onActivityResult() method in the classList is called.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == 0) {
if (data.hasExtra("com.naomi.classAlert.classList.course")) {
Toast.makeText(this, data.getExtras().getString("com.naomi.classAlert.classList.course"),
Toast.LENGTH_SHORT).show();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With