|
| 1 | +package com.example.plugintest.activity; |
| 2 | + |
| 3 | +import com.example.plugintest.R; |
| 4 | +import com.plugin.core.PluginLoader; |
| 5 | + |
| 6 | +import android.app.Activity; |
| 7 | +import android.content.Context; |
| 8 | +import android.os.Bundle; |
| 9 | +import android.util.Log; |
| 10 | +import android.view.LayoutInflater; |
| 11 | +import android.view.Menu; |
| 12 | +import android.view.MenuItem; |
| 13 | +import android.view.View; |
| 14 | +import android.view.View.OnClickListener; |
| 15 | +import android.view.ViewGroup; |
| 16 | +import android.widget.Button; |
| 17 | +import android.widget.Toast; |
| 18 | + |
| 19 | +/** |
| 20 | + * 完整生命周期模式 不使用反射、也不使用代理,真真正证实现activity无需在Manifest中注册! |
| 21 | + * |
| 22 | + * @author cailiming |
| 23 | + * |
| 24 | + */ |
| 25 | +public class PluginForOppoAndVivoActivity extends Activity implements OnClickListener { |
| 26 | + |
| 27 | + private ViewGroup mRoot; |
| 28 | + private LayoutInflater mInflater; |
| 29 | + |
| 30 | + @Override |
| 31 | + public void onCreate(Bundle savedInstanceState) { |
| 32 | + super.onCreate(savedInstanceState); |
| 33 | + setTitle("测试插件中拥有真正生命周期的Activity"); |
| 34 | + mInflater = getLayoutInflater(); |
| 35 | + View scrollview = mInflater.inflate(R.layout.plugin_layout, null); |
| 36 | + |
| 37 | + mRoot = (ViewGroup) scrollview.findViewById(R.id.content); |
| 38 | + |
| 39 | + initViews(); |
| 40 | + |
| 41 | + setContentView(scrollview); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 46 | + menu.add(0,0, 0, "test plugin menu"); |
| 47 | + return super.onCreateOptionsMenu(menu); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 52 | + Toast.makeText(this, "test plugin menu", Toast.LENGTH_LONG).show(); |
| 53 | + Log.e("xx", "" + item.getTitle()); |
| 54 | + return super.onOptionsItemSelected(item); |
| 55 | + } |
| 56 | + |
| 57 | + public void initViews() { |
| 58 | + |
| 59 | + Button btn1 = (Button) mRoot.findViewById(R.id.plugin_test_btn1); |
| 60 | + btn1.setOnClickListener(this); |
| 61 | + |
| 62 | + Button btn2 = (Button) mRoot.findViewById(R.id.plugin_test_btn2); |
| 63 | + btn2.setOnClickListener(this); |
| 64 | + |
| 65 | + Button btn3 = (Button) mRoot.findViewById(R.id.plugin_test_btn3); |
| 66 | + btn3.setOnClickListener(this); |
| 67 | + |
| 68 | + Button btn4 = (Button) mRoot.findViewById(R.id.plugin_test_btn4); |
| 69 | + btn4.setOnClickListener(this); |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void onClick(View v) { |
| 75 | + Log.v("v.click 111", "" + v.getId()); |
| 76 | + if (v.getId() == R.id.plugin_test_btn1) { |
| 77 | + View view = mInflater.inflate(R.layout.plugin_layout, null, false); |
| 78 | + mRoot.addView(view); |
| 79 | + Toast.makeText(this, getString(R.string.hello_world1), Toast.LENGTH_LONG).show(); |
| 80 | + } else if (v.getId() == R.id.plugin_test_btn2) { |
| 81 | + View view = mInflater.inflate(com.example.pluginsharelib.R.layout.share_main, null, false); |
| 82 | + mRoot.addView(view); |
| 83 | + Toast.makeText(this, getString(com.example.pluginsharelib.R.string.share_string_1), Toast.LENGTH_LONG).show(); |
| 84 | + } else if (v.getId() == R.id.plugin_test_btn4) { |
| 85 | + ((Button) v).setText(getResources().getString(com.example.pluginsharelib.R.string.share_string_2)); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * 如果不用代理activity,而是要拥有完整生命周期,需重写如下方法 |
| 91 | + */ |
| 92 | + @Override |
| 93 | + protected void attachBaseContext(Context newBase) { |
| 94 | + super.attachBaseContext(PluginLoader.getDefaultPluginContext(PluginForOppoAndVivoActivity.class)); |
| 95 | + } |
| 96 | +} |
0 commit comments