android开发中, 关闭另一个activity6
发布网友
发布时间:2023-11-30 07:10
我来回答
共3个回答
热心网友
时间:2024-07-14 16:41
楼上说的很对的。
this可以理解为就是A_activity这个类;
一般就可以定义到这里,可以看一下activity的生命周期就晓得了,其他地方大部分也可以,就是别定义到onPause或者各种listener中就行了。多看看生命周期。
另外,public static A_activity mc= null,这种方式不好,容易造成内存泄漏,而且很容易造成此activity无法关闭。
热心网友
时间:2024-07-14 16:41
"mc=this"."this"指的就是该类的实例(也叫做类的对象)。在这里的意思就是在变量mc中存储下"A_activity"的引用,然后在B_Activity中才可调用其的finish()方法。
不一定非得在onCreate里面定义,也可在自定义的方法中定义,意义是一样的。
这不是一个android的特性,而是Java语言的一个特性,"this"关键字只能在方法内部使用,表示对“调用方法的那个对象”的引用。
楼主可参考下《Java编程思想》中"this关键字"这一节。
追问这样啊, 就是说this通常是指代当前的类对象?
那么这一句: Intent(this,xxx.class);
就是在用类名显示跳转的时候, setClass的第一个参数不是应该是包名吗? 这里的this指什么呢?
追答Intent setClass (Context packageContext, Class cls)
API中,第一个参数不是包名,而是一个Context类型,而Activity是继承Context的。那“this”代表的就是当前的Activity。
热心网友
时间:2024-07-14 16:42
我给你发一个吧:这个是activity的管理,所有的activity都继承自一个模板
package com.xxx.sh;
import java.util.LinkedList;
import android.app.Activity;
import android.text.TextUtils;
public class ActivityManager {
private static LinkedList<Activity> activityStack;
private static ActivityManager instance;
private ActivityManager() {
}
public static ActivityManager getScreenManager() {
if (instance == null) {
instance = new ActivityManager();
}
return instance;
}
/**
* 退出栈顶Activity
*
* @param activity
*/
public void popActivity(Activity activity) {
if (activity != null) {
activityStack.remove(activity);
LOG.i("Activity管理", activity.getClass().getSimpleName() + "弹出栈");
activity.finish();
activity = null;
}
}
/**
* 获得当前栈顶Activity
*
* @return
*/
public Activity currentActivity() {
Activity activity = null;
if (activityStack != null && !activityStack.isEmpty())
activity = activityStack.get(activityStack.size() - 1);
return activity;
}
/**
* 将当前Activity推入栈中
*
* @param activity
*/
public void pushActivity(Activity activity) {
if (activityStack == null) {
activityStack = new LinkedList<Activity>();
}
activityStack.add(activity);
LOG.i("Activity管理", activity.getClass().getSimpleName() + "加入栈中");
}
/**
* 退出栈中所有Activity
*/
public void popAllActivityExceptOne() {
while (true) {
Activity activity = currentActivity();
if (null == activity) {
break;
}
// if (activity != null) {
// activity.finish();
// }
popActivity(activity);
}
}
public void popAllActivity() {
while (true) {
Activity activity = currentActivity();
if (null == activity) {
break;
}
// if (activity != null) {
// activity.finish();
// }
popActivity(activity);
}
}
/**
* 退出指定名字的activity
*/
public void popPointNameActivity(String name) {
while (true) {
Activity activity = currentActivity();
if (null == activity) {
break;
}
String activityName = activity.getComponentName().getClassName().toString();
if (TextUtils.equals(name, activityName)) {
continue;
}
// if (activity != null) {
// activity.finish();
// }
popActivity(activity);
}
}
/**
* 获得当前ACTIVITY 名字
*/
public String getCurrentActivityName() {
Activity activity = currentActivity();
String name = "";
if (activity != null) {
name = activity.getComponentName().getClassName().toString();
}
return name;
}
public boolean isCatalogActivity() {
for (int j = 0; j < activityStack.size(); j++) {
Activity activity = activityStack.get(j);
if (activity instanceof CatalogActivity) {
return true;
}
}
return false;
}
// 返回大厅页面
public void popToCatalogActivity() {
while (true) {
Activity activity = currentActivity();
LOG.i("Activity:", activity.getClass().getSimpleName() + "预报弹出栈");
if (activity instanceof CatalogActivity) {
break;
}
popActivity(activity);
}
}
}
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.LightingColorFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Toast;
@SuppressLint("NewApi")
public class TemplateActivity extends Activity {
public CustomProgressDialog proDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityManager.getScreenManager().pushActivity(this);
proDialog = new CustomProgressDialog(this);
proDialog.dismiss();
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
getWindowWidth();
}
public void StopProgress() {
if (proDialog != null)
proDialog.dismiss();
}
public void StartProgress() {
if (proDialog == null)
proDialog = new CustomProgressDialog(this);
proDialog.setCanceledOnTouchOutside(false);
proDialog.show();
}
public void showNetError() {
Toast.makeText(this, "网络连接失败,请检查当前网络配置。 ", Toast.LENGTH_SHORT).show();
}
public boolean isInternet() {
ConnectivityManager conManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = conManager.getActiveNetworkInfo();
if (networkInfo != null) {
return networkInfo.isAvailable();
}
return false;
}
public final static OnTouchListener touchLight = new OnTouchListener() {
// public final float[] BT_SELECTED = new float[] { 1, 0, 0, 0, 50, 0,
// 1,
// 0, 0, 50, 0, 0, 1, 0, 50, 0, 0, 0, 1, 0 };
public final float[] BT_NOT_SELECTED = new float[] { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0 };
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
v.getBackground().setColorFilter(new LightingColorFilter(0xEEEEEFF, 0xFFAA0000));
v.setBackgroundDrawable(v.getBackground());
} else if (event.getAction() == MotionEvent.ACTION_UP) {
v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_NOT_SELECTED));
v.setBackgroundDrawable(v.getBackground());
}
return false;
}
};
public boolean isWifiConnection() {
final ConnectivityManager connMgr = (ConnectivityManager) getApplicationContext().getSystemService(
Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifi.isAvailable())
return true;
else
return false;
}
public boolean is3GConnection() {
final ConnectivityManager connMgr = (ConnectivityManager) getApplicationContext().getSystemService(
Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mobile.isAvailable())
return true;
else
return false;
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
getWindowWidth();
}
public void getWindowWidth() {
DisplayMetrics DM = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(DM);
// LogUtils.i("h=" + DM.heightPixels + "w=" + DM.widthPixels);
Constance.SCREEN_DM_HIGHT = DM.heightPixels;
if (Constance.SCREEN_DM_WIDTH == 0)
Constance.SCREEN_DM_WIDTH = DM.widthPixels;
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onRestart() {
Log.d("AbstractTemplateActivity", "onRestart");
super.onRestart();
}
@Override
public void onLowMemory() {
Log.d("AbstractTemplateActivity", "onLowMemory");
super.onLowMemory();
}
@Override
protected void onResume() {
Log.d("AbstractTemplateActivity", "onResume");
super.onResume();
// MobclickAgent.onResume(this);
}
public void onPause() {
Log.d("AbstractTemplateActivity", "onPause");
super.onPause();
}
@Override
public void onBackPressed() {
super.onBackPressed();
ActivityManager.getScreenManager().popActivity(this);
}
}追问- - ! 这么长? 我是在学习阶段。。 能用但是不懂啊。。
追答如果你自己试着玩,怎么都行。如果你工作中采用,我给你的是比较好的选择了