I've created some activities for my project, but I have some problems when go from one activity to another
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transaksibaru);
getSupportActionBar().show();
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
radioGroup = (RadioGroup) findViewById(R.id.editJenis);
KeteranganTrx = (EditText)findViewById(R.id.editKeterangan);
JumlahTrx = (EditText)findViewById(R.id.editJumlah);
JumlahTrx.addTextChangedListener(onTextChangedListener());
SubmitTransaksi = (TextView) findViewById(R.id.buttonSubmit);
SubmitTransaksi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Checking whether EditText is Empty or Not
CheckEditTextIsEmptyOrNot();
if(CheckEditText){
// If EditText is not empty and CheckEditText = True then this block will execute.
ProsesTransaksi(JenisTrxHolder,KeteranganTrxHolder, JumlahTrxHolder);
}
else {
// If EditText is empty then this block will execute .
Toast.makeText(TransaksiBaru.this,"Pastikan Semua Kolom Terisi", Toast.LENGTH_SHORT).show();
}
}
});
}
private TextWatcher onTextChangedListener() {
return new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
JumlahTrx.removeTextChangedListener(this);
try {
String originalString = s.toString();
Long longval;
if (originalString.contains(",")) {
originalString = originalString.replaceAll(",", "");
}
longval = Long.parseLong(originalString);
DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(Locale.US);
formatter.applyPattern("#,###,###,###");
String formattedString = formatter.format(longval);
//setting text after format to EditText
JumlahTrx.setText(formattedString);
JumlahTrx.setSelection(JumlahTrx.getText().length());
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
}
JumlahTrx.addTextChangedListener(this);
}
};
}
public void ProsesTransaksi(final String S_Jenis, final String S_Keterangan, final String S_Jumlah){
class ProsesTransaksiClass extends AsyncTask<String,Void,String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(TransaksiBaru.this,"Loading Data",null,true,true);
}
@Override
protected void onPostExecute(String httpResponseMsg) {
super.onPostExecute(httpResponseMsg);
progressDialog.dismiss();
if(httpResponseMsg.equalsIgnoreCase("Transaksi berhasil di simpan")){
Toast.makeText(TransaksiBaru.this,httpResponseMsg.toString(), Toast.LENGTH_SHORT).show();
finish();
Intent IntentDashboard = new Intent(getApplicationContext(),UserDashboard.class);
startActivity(IntentDashboard);
}
else{
Toast.makeText(TransaksiBaru.this,httpResponseMsg.toString(), Toast.LENGTH_SHORT).show();
}
}
@Override
protected String doInBackground(String... params) {
hashMap.put("JenisTrx",params[0]);
hashMap.put("KeteranganTrx",params[1]);
String a = params[2];
a=a.replaceAll(",","");
hashMap.put("JumlahTrx",a);
finalResult = httpParse.postRequest(hashMap, HttpURL);
return finalResult;
}
}
ProsesTransaksiClass prosesTransaksiClass = new ProsesTransaksiClass();
prosesTransaksiClass.execute(S_Jenis,S_Keterangan,S_Jumlah);
}
public void CheckEditTextIsEmptyOrNot(){
int selectedId = radioGroup.getCheckedRadioButtonId();
JenisTrx = (RadioButton) findViewById(selectedId);
JenisTrxHolder = JenisTrx.getText().toString();
KeteranganTrxHolder = KeteranganTrx.getText().toString();
JumlahTrxHolder = JumlahTrx.getText().toString();
if(TextUtils.isEmpty(JenisTrxHolder) || TextUtils.isEmpty(KeteranganTrxHolder) || TextUtils.isEmpty(JumlahTrxHolder))
{
CheckEditText = false;
}
else {
CheckEditText = true ;
}
}
@Override
public void onBackPressed()
{
this.finish();
}
}
When I press the back key on my device, it directly closes the app. What I want is when I press the key back in my device it will back to the previous activity. How to detect if there is previous activity then true back to previous activity.
Any help with this?
@Override
protected void onPostExecute(String httpResponseMsg) {
super.onPostExecute(httpResponseMsg);
progressDialog.dismiss();
if(httpResponseMsg.equalsIgnoreCase("Transaksi berhasil di simpan")){
Toast.makeText(TransaksiBaru.this,httpResponseMsg.toString(),
Toast.LENGTH_SHORT).show();
finish();// Remove this line from your asynctask.
Intent IntentDashboard = new Intent(getApplicationContext(),UserDashboard.class);
startActivity(IntentDashboard);
} else {
Toast.makeText(TransaksiBaru.this,httpResponseMsg.toString(),
Toast.LENGTH_SHORT).show();
}
}