Shown in the following video, MainActivity.this will change when activity destroyed and re-created, getApplicationContext() will change when the application killed and re-started.
MainActivity.java
package com.example.androidtestcontext;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView info1 = (TextView)findViewById(R.id.info1);
TextView info2 = (TextView)findViewById(R.id.info2);
String s1 = "MainActivity.this:\n"
+ "getClass() = " + MainActivity.this.getClass() + "\n"
+ MainActivity.this;
info1.setText(s1);
String s2 = "getApplicationContext():\n"
+ "getClass() = " + getApplicationContext().getClass() + "\n"
+ getApplicationContext();
info2.setText(s2);
}
}
layout
web support
www.klikcover.com
Tidak ada komentar:
Posting Komentar