-1

Been trying to get my app to go back to the main activity when a user touches the OK button inside an AlertDialog.

The alert dialog I show is basically an error message to inform the user that no data was found for a specific date they chose and called in an activity that is NOT the main activity but I want them sent back to the main one when they click OK.

The call runs fine, its just that it seems to not know how to find the Main Activity even though the call I am using works in any normal activity.

The error I get is:

 1554-1554/org.springframework.android.showcase2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=org.springframework.android.showcase2.MainActivity }
        at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1545)

Specific AlertDialog code is:

package org.springframework.android.showcase2;

import android.app.ActionBar;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MyAlertDialogFragment extends DialogFragment {
    private String title;
    private String message;
    private TextView tvError;

    public MyAlertDialogFragment(String sTitle, String sMessage){
        this.title = sTitle;
        this.message = sMessage;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        return new AlertDialog.Builder(getActivity())
                .setTitle(this.title)
                .setMessage(this.message)
                .setPositiveButton("Ok",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            // This is the way I nomally call a new activity inside other activities but seems to not work inside this AlertDialog scope
                            Intent i = new Intent("org.springframework.android.showcase2.MainActivity");
                            startActivity(i);
                        }
                    }
            )
            .create();
    }

}
Namrata
  • 1,683
  • 1
  • 17
  • 28
John Cogan
  • 1,034
  • 4
  • 16
  • 39

2 Answers2

4

Code line

// This is the way I nomally call a new activity inside other activities but seems to not work inside this AlertDialog scope
Intent i = new Intent("org.springframework.android.showcase2.MainActivity");
startActivity(i);

is not correct.

When you say new Intent("org.springframework.android.showcase2.MainActivity"), it means you are lanching an activity which can handle "org.springframework.android.showcase2.MainActivity" Action.

But in your case "org.springframework.android.showcase2.MainActivity" represents an Activity not an Action.

So use

Intent i = new Intent(getActivity(), org.springframework.android.showcase2.MainActivity.class);
startActivity(i);

You should read How to start an Activity and Start an activity from a fragment.

Community
  • 1
  • 1
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
  • @ρяσѕρєяK I didn't say. Please read my description. And yes I am agree with your comment that if he add action into manifest for the activity, his code will work. Thats another way. Normally we create some meaningful actions. But his code was looking like he is trying to add component not an action. And again I am stuck with the question "is the selected answer correct?" – Pankaj Kumar Feb 20 '14 at 10:07
1

You go wrong over here.You need to passed context into your Intent

Intent i = new Intent(getActivity(),"org.springframework.android.showcase2.MainActivity");
startActivity(i);

When you do this:

Intent in = new Intent(getActivity(), SecondActivity.class);

You are creating an explicit Intent specifying the component SecondActivity. The signature for this method is Intent(Context packageContext, Class clas). It uses the package name from packageContext and the class name from clas to create an explicit Intent for that component. If you use this constructor inside an Activity, you can just use this as the first parameter, because Activity extends Context. If you use this constructor from another class (like an OnClickListener) you need to specify MyActivity.this as the first parameter to pass the instance of the Activity and not of the OnClickListener (because OnClickListener does not extend Context).

M D
  • 47,665
  • 9
  • 93
  • 114
  • getActivity returns the activity this fragment is associated with – Raghunandan Feb 20 '14 at 09:47
  • 1
    then why would this be wrong `new AlertDialog.Builder(getActivity())`? – Raghunandan Feb 20 '14 at 09:48
  • Thanks guys, the combined help from M D and Raghunandan has worked. Edit my code as follows: Intent i = new Intent(getActivity(), org.springframework.android.showcase2.MainActivity.class); and that made it work. – John Cogan Feb 20 '14 at 09:55
  • @JohnCogan : you are doing in right way just forget to add action in Activity AndroidManifest as previous told in my comment. all answers right but not saying what is issue in your code and telling you another ways for starting Activity. – ρяσѕρєя K Feb 20 '14 at 09:59
  • @MD That was not for you. That was for JohnCogan. He is saying that your effort helped him, although you posted wrong answer before. This seems that question was posted by a group and selected. He should change his behavior. – Pankaj Kumar Feb 20 '14 at 10:00