-4

I have a class which does not extend Activity. And in that class, I use a try-catch block to catch exceptions. How can I pass any exceptions (the Exception e) caught in that block to another Activity? My application checks with a boolean flag to display a toast message. But how do I pass this message to another activity?

public class AgAppHelperMethods   {

  private static AgAppHelperMethods instance = null;
  static boolean flag=true;

 public static   String[][] AgAppXMLParser( String parUrl) {



    String _node,_element;
    String[][] xmlRespone = null;
    try {
  String url = www.xyz.com
            URL finalUrl = new URL(url);


    catch (Exception e)
    {
      flag=false;
      Log.e( "CONNECTION ERROR  SERVER NOT RESPONDING", e);
    } 


          public class LoginScreen extends Activity implements Serializable {


public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    setContentView(R.layout.agapplogin);


       btnClear.setOnClickListener(new OnClickListener() {
        public void onClick(View v)

        {
            postLoginData();
        }

    });


           public void postLoginData()

{
               xmlRespone = AgAppHelperMethods.AgAppXMLParser( url);

        if(!AgAppHelperMethods.flag)
        {
             Toast.makeText(getApplicationContext(), "Error  server 
    not responding " , Toast.LENGTH_SHORT).show();
             myProgressDialog.dismiss();
        }
Siddharth Lele
  • 27,623
  • 15
  • 98
  • 151
Hayya ANAM
  • 575
  • 2
  • 14
  • 38

2 Answers2

2
 catch (Exception e) {        
    flag=false;
    Log.e( "CONNECTION ERROR  SERVER NOT RESPONDING", e);

    String theException = e.getMessage();

    Bundle b = new Bundle();        
    b.putString("Exception", theException); //or you can replace theException with a custom message

    Intent nextActivity = new Intent(MyActivity.this, NewActivity.class);
    nextActivity.putExtras(b);
    startActivity(nextActivity); 
    }   

and you can receive it on the onCreate method of your next activity

    Bundle b = getIntent().getExtras();
    String error = b.getString("Exception");
    Toast.makeText(NewActivity.this, error, Toast.LENGTH_SHORT).show();

Hope this helps!!

ThePCWizard
  • 3,338
  • 2
  • 21
  • 32
0

Use Intent extras to communicate extra data to another Activity:

See here: How do I get extra data from intent on Android? and here: http://www.vogella.com/articles/AndroidIntent/article.html

Community
  • 1
  • 1
RvdK
  • 19,580
  • 4
  • 64
  • 107