Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7532957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:36:09+00:00 2026-05-30T05:36:09+00:00

This is my problem, I have the main view which only shows one button,

  • 0

This is my problem, I have the main view which only shows one button, pressing this button another view is shown. This view has only another button, when this button is push this current view finishs and the control backs to the previous view.

To show the second view I use startActivityForResult, I put the code here.

private void startNewview() {       
    Intent it = new Intent(getApplicationContext(), newView.class);
    startActivityForResult(it,VIEW_ID);

}

The view called only has a button event, here is the code

Button b = (Button) findViewById(R.id.close);
    b.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            setResult(RESULT_OK);               
            finish();

        }
    });

And finally, the method onActivityResult in the main view, here is the code

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == VIEW_ID && resultCode == RESULT_OK) {
        tv = (TextView) findViewById(R.id.tv);
        tv.setText("The result ok is  here :)");
    }

}

The problem is resultCode always is 0 = RESULT_CANCELED and I do not know how to solve it, can anyone help me?

Thank you very much!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T05:36:10+00:00Added an answer on May 30, 2026 at 5:36 am

    I can’t explain what is happening in your code but I have a project sample to do this..

    a FooActivity with just a button btnFoo:

    @Override
    protected void onStart() {
        super.onStart();
    
        btnFoo.setOnClickListener( new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent("xper.activity.ACTIVITY_BAR_INTENT"),1);
            }
        });
    }
    

    and an BarActivity added in the AndroidManifest.xml like that:

    <activity
        android:name = "BarActivity">
        <intent-filter>
            <action
                android:name = "xper.activity.ACTIVITY_BAR_INTENT"/>
            <category
                android:name = "android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>
    

    The respective code to retrieve the intent inside the bar is in the onClicEvent of the btnBar (Button):

    @Override
    protected void onStart() {
        super.onStart();
    
        btnBar.setOnClickListener( new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("xper.activity.ACTIVITY_BAR_RESULT_INTENT");
                intent.putExtra("codBar", "bar");
                setResult(Activity.RESULT_OK, intent);
                finish();
    
            }
        });
    }
    

    Now, if you doesn’t handled well the onActivityResult() event, when you press the Android button “BACK”, you can get errors.

    If the Intent (intention) in the Activity B is to give some information to the activity A, if you press the button back, I don’t know if the activity B will be in the stack, but the intention isn’t done. So I did the following:

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
    
        //Intent intent = new Intent("xper.activity.ACTIVITY_BAR_RESULT_INTENT");
        //intent.putExtra("codBar", "bar");
        //setResult(Activity.RESULT_CANCELED, intent);
        setResult(Activity.RESULT_CANCELED);
        finish();
    }
    

    Handling the information I did the following in the event onActivityResult() to see the retrieved information in the Bar Activity:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(data != null) {
            Toast.makeText(this, "BAR\trequestCode == " + requestCode + "\tresultCode  == " + resultCode + "\tdata == " + data, 10000).show();
            btnFoo.setText("BAR\trequestCode == " + requestCode + "\tresultCode  == " + resultCode + "\tdata == " + data /*+ "extras == " + data.getExtras().getString("codBar")*/);
        } else {
            Toast.makeText(this, "BAR\trequestCode == " + requestCode + "\tresultCode  == " + resultCode, 10000).show();
            btnFoo.setText("BAR\trequestCode == " + requestCode + "\tresultCode  == " + resultCode);
        }
    
    }
    

    if you have more activities to return infomation to the parent activity is good pratices do the following:

    //put private static final int globals atributes with the respective name of the
    //activity to represent the requestCode for each activity you have like:
    private static final int ACTIVITY1 = 117;
    private static final int ACTIVITY2 = 118;
    ...
    private static final int ACTIVITYN = 215;
    
    //In the event onActivityResult() is better use the switch statement to handle each
    //specific activity to catch information
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        if(resultCode == Activity.RESULT_CANCELED) return; // breaks
        //if you decide to handle some information of Activity.RESULT_CANCELED
        //ignore the above condition that returns and handle it inside the switch statement
    
        switch(requestCode) {
        case ACTIVITY1:
        {
            //Dosomething
        } break;
        case ACTIVITY2:
        {
            //Dosomething
        } break;
        ...
        case ACTIVITYN:
        {
            //Dosomething
        } break;
        }
    }
    

    If you can’t do this sample code..
    please give me your email for me send the FooBarActivity project

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this problem: I have a program which should fetch (let's say) 100 records
I hava a main view, where you have one object and you can change
I am dealing the view flipper with three views. The only problem which I
I have a Kohana framework which is building ExtJS objects in one view, which
I'm writing an application which have two modes; one view mode and one edit
I have the main View controller which adds 2 subviews. - (void)viewDidLoad { [self
I have a simple app which adds a subview over the main view when
Okay. I'm sick of this problem. This has to have an easy fix, I'm
NOTE: I have solved the majority of this problem but have run into a
I am wondering how you would approach this problem I have two Taxrates that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.