2

Im trying to return from the about dialog to the main activity by a button click:

public class AboutActivity extends Activity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.about);
}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    SharedPreferences prefs = getSharedPreferences("com.example.tiocontas",MODE_PRIVATE);
    SharedPreferences.Editor prefsEditor = prefs.edit();
    prefsEditor.putBoolean("FirstTime", false);
    finish();
    //this.onBackPressed();
}
}

I've tried both finish() and onBackPressed() with no results, im doing something wrong could someone give me some hints?

João Terramoto
  • 25
  • 1
  • 1
  • 4
  • 2
    Is your `onClick()` attached to your button in some way that isn't shown, through xml or code not shown? What happens when you click on the button, I assume just nothing? – codeMagic Feb 19 '13 at 20:35

3 Answers3

4

From what I can see in your code, you don't have a button attached to your onClick() method. You can do this two ways, in xml or programmatically.

In xml

<Button
...
android:onClick="functionName"/>

Then in your code, define your function which you named in your xml

public void functionName(View v)
{
  // some code
  finish();
}

Programmatically, declare your button

Button aBtn = (Button) findViewById(R.id.button_id);
aBtn.setOnClickListener(new OnClickListener() {         
    @Override
    public void onClick(View v)
    {
      // some code
      AboutActivity.this.finish()
    }
});

If you have already attached your button to the onClick() in some way not shown then you may be finishing your main activity. In which case, describe what happens when you click the button and show your Main Activity

Docs for OnClickListener()

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • Sorry, i thought this would listen for any button and that it would be identified by the onClick(View arg0) variable. Its working now, thank you! – João Terramoto Feb 19 '13 at 21:18
  • Nope, it needs to be attached to the listener in some way so the function knows which button to execute that code on, otherwise you wouldn't be able to use multiple `onClick()`s in an `Activity`. Glad I was able to help – codeMagic Feb 19 '13 at 21:23
1

It seems like you have already finished; your previous Activity. Remove finish() from MainActivity where you got to your aboutActiivty.

wtsang02
  • 18,603
  • 10
  • 49
  • 67
1

You can call onBackPressed() function on btnclick and then can define the following code at end in your class:

  @Override
    public void onBackPressed() {
        super.onBackPressed();
        Intent intent = new Intent(this, YourActivity.class);
        startActivity(intent);
    }

hope this will help you.

wtsang02
  • 18,603
  • 10
  • 49
  • 67
Akmal Rasool
  • 496
  • 2
  • 7
  • 17