2

Answered by Intent With Extras

I have an upload activity with two results returned by the server.

Result 1: Data Upload Successful. Result 2: Upload Not Successful.

I added a third result of intent not being received (e.g device data off, or device has no data bundles). How can i return these and set textviews to a message of these depending on what was returned?

ResultsACtivity.java

public class SuccessActivity extends AppCompatActivity {
    public TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.success_main);
        getSupportActionBar().setTitle("Data Upload Results");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        textView = findViewById(R.id.experiences);
        String data;
        String data2 = "Your Data Upload Was NOT Successfull!!";

        Intent intent = getIntent();
        Bundle bundle = getIntent().getExtras();

        if (bundle.containsKey("VAL1")) {
            data = bundle.getString("VAL1");
            textView.setText(data);
        }
        else {
            textView.setText(data2);
        }

VAL1 is data results returned by the server. (1. "Upload Successful" or null.

NyP
  • 499
  • 4
  • 18

2 Answers2

0

I recommend you use an enum for this:

enum UploadStatus {
   SUCCESSFUL, UNSUCCESSFUL
}

You can pass this with the intent as a serializable extra, where you start your SuccessActivity from:

intent.putExtra("status", UploadStatus.SUCCESSFUL)

In SuccessActivity you can then read out this value from the intent's extras and set the status text based on that:

Intent intent = getIntent();
UploadStatus status = (UploadStatus) intent.getSerializableExtra("status");

if (intent == UploadStatus.SUCCESSFUL) {
    textView.setText("Your Data Upload was successful");
} 
else {
    textView.setText("Your Data Upload Was NOT Successfull!!");
} 
Jaran
  • 313
  • 1
  • 2
  • 10
  • Thanks @Jaran. This seems a little complicated. The ammended code is working fr successful upload and outputs a string from the server. But it does not work where the server doesnt respont or device has no data. – NyP Mar 05 '20 at 09:47
  • I would recommend adding a layer between your backend communication and frontend which parses response from the server and handle any faults. That way you can parse responses, handle connection states and provide meaningful values to the frontend layer of your application. – Jaran Mar 05 '20 at 13:12
  • how is that implementable sirr. I edited to include other codes – NyP Mar 05 '20 at 13:30
0

Please use this code

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

    TextView textView = findViewById(R.id.experiences);

    String dataerror = "Your Data Upload Was NOT Successfull!!";

    Intent intent = getIntent();
    if(intent != null) {
        String data = intent.getStringExtra("VAL1"); //  Thats the right way to get the extra String;
        if(data != null && !data.isEmpty()) {
            textView.setText(data);
        } else {
            textView.setText(dataerror);
        }
    } else {
        textView.setText(dataerror);
    }
}
shinsky Paul
  • 97
  • 10