0

I have an upload activity that can result to 3 options. Result 1: Successful Upload of data. Result 2: Upload not Successful.

Previous Activity Code

protected void onPostExecute(String string1) {
super.onPostExecute(string1);
progressDialog.dismiss();
final Intent intent = new Intent(UploadActivity.this, SuccessActivity.class);
intent.putExtra("VAL1", string1);
startActivity(intent);
 } 
catch (Exception e) {
e.printStackTrace();
}

Mysql Results code

if(mysqli_query($con,$sql)){
       //Data To Be Uploaded.........
        echo "Your Data Was Successfully Uploaded. Thank You";
    }
    mysqli_close($con);
}else{ }

ResultsActivity Android

public class SuccessActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.success_main);

Intent intent = getIntent();
Bundle extras = intent.getExtras();
String data = null;
String data2 = "Your Data Upload Was NOT Successfull!!";

if (getIntent()!= null && getIntent().getExtras() != null) {

       if (extras.getString("val1") != null) {
                data = extras.getString("val1");
                textView = findViewById(R.id.experiences);
                textView.setText(data);
            }
       else if(getIntent() != null && getIntent().getExtras() == null) {
                textView = findViewById(R.id.experiences);
                textView.setText(data2);
            }
       else if (getIntent() == null && getIntent().getExtras() == null){
                textView = findViewById(R.id.experiences);
                textView.setText(data2);
            }
        }
NyP
  • 499
  • 4
  • 18
  • 1
    Where are the putExtra()'s? – blackapps Mar 02 '20 at 19:27
  • 1
    You should not let this activity decide if an upload is succes full or not but send it the right message with putExtra like `putExtra("Data Upload Not Successfull!!");` or what ever. – blackapps Mar 02 '20 at 19:30
  • `If(extras.hasKey("val1")) //success expression ` – AA Shakil Mar 03 '20 at 08:26
  • I have edited the code to suggest what i need – NyP Mar 03 '20 at 15:21
  • Why are you calling `getIntent()` so many times instead of variable `intent` that you have already declared. – Pratik Butani Mar 04 '20 at 13:32
  • @pratik butani. I hear you. Do you get what I want to achieve first? – NyP Mar 04 '20 at 16:34
  • Where you have called `putExtra`? – Pratik Butani Mar 05 '20 at 04:42
  • @NyP what is it that you require, or are trying to achieve? – electrocrat Mar 05 '20 at 07:19
  • @electrocrat 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? – NyP Mar 05 '20 at 08:01
  • So the `UploadActivity` launches the above `SuccessActivity` to display the result. Your code is a bit redundant but it should work fine. Unless as others have stated that the `putExtra` from the `UploadActivity` is not working. So share that code too. – electrocrat Mar 05 '20 at 12:48
  • @electrocrat see my edited quiz – NyP Mar 05 '20 at 13:12

1 Answers1

1

Referring to this: How do I get extra data from intent on Android?

Your extra String "VAL1" is not in a Bundle.

Change your SuccessActivity code to this and try:

@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);
    }
}
electrocrat
  • 446
  • 3
  • 8
  • Thanks for the efforts but its not working. When i put data off before upload or rename the upload script in the server the textview is still empty instead of outputting the Not Successful Message – NyP Mar 05 '20 at 15:18
  • Is `SuccessActivity` opening up? – electrocrat Mar 05 '20 at 15:33
  • Yes it is opening but has empty textview instead of outputting the Not Successful Message. For successful upload it is ok just like it was doing in the initial code – NyP Mar 05 '20 at 15:43
  • Maybe the String result is empty? Try adding a `&& !data.isEmpty()` to that `if(data != null)` check. – electrocrat Mar 05 '20 at 15:47
  • String data = intent.getStringExtra("VAL1"); was to return the success message any other condition to return dataerror message in your code above. Can we try a code for null intent? – NyP Mar 05 '20 at 15:51
  • Yes, the first check is to see if `intent = null`. – electrocrat Mar 05 '20 at 15:53
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/209098/discussion-between-electrocrat-and-nyp). – electrocrat Mar 05 '20 at 15:53