2

in the following code :

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btn = (Button) findViewById(R.id.btn1);


        // On button click call this listener
        btn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {


                Thread background = new Thread(new Runnable() {
                    public void run() {
                        //Do some work here !
                    }};
                };

above in the code the new new OnClickListener() is an Anonymous Inner Classes inside the class MainActivity, but what about the new Thread(new Runnable() is this class also an inner class and which class is the outer class for it ??? and if it's an inner class inside new OnClickListener() can this class access the MainActivity Members ?

Mohamad Ghanem
  • 599
  • 2
  • 8
  • 25

1 Answers1

0

I don't want to sound snarky, but if you don't know this then you probably shouldn't be attempting to start threads inside of an Activity. Android is pretty peculiar when it comes to Thread life cycles and how you should perform background work. Generally, any background work should not be done in a Thread. At a minimum it should be done in an AsyncTask and better yet in a Service that can provide proper lifecycle tracking of the work units.

To directly answer your question check out the documentation on Nested Classes and this answer.

Community
  • 1
  • 1
Rich Schuler
  • 41,814
  • 6
  • 72
  • 59