本文介绍了抽象类扩展了多种类型的活动的Java和Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在的Java和Android编程新手。
我刚刚创建与登录表单,并登记表简单的应用程序。我的登录表单,并登记表,现在的作品。
我创建了自Activity类扩展抽象类SignedActivity。我想每个活动是可用的登录从SignedActivity继承后。如果从活动继承SignedActivity我可以肯定,它仅仅是供用户签署

I am newbie in Java and Android programming.I just created simple app with login form and registration form. My login form and registration form works right now.I created abstract SignedActivity class which extends from Activity class. I want each activity which is available after login inherits from SignedActivity. If activity inherits from SignedActivity I can be sure that it is available only for signed users.

这是SignedActivity的样子:

This is how SignedActivity looks:

public abstract class SignedActivity extends Activity {

public static String LOG_OUT_PATH = "/logOut.php";

private String sessionId;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (savedInstanceState != null) {
        String newSessionId = savedInstanceState.getString("sessionId");
        if (newSessionId instanceof String) {
            sessionId = newSessionId;
            return;
        }
    }
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        String newSessionId = extras.getString("sessionId");
        if (newSessionId instanceof String) {
            sessionId = newSessionId;
            return;
        }
    }

    finish();
}

public void startSignedActivity(Intent intent) {
    intent.putExtra("sessionId", getSessionId());
    this.startActivity(intent);
}

protected String getSessionId() {
    return sessionId;
}

private void setSessionId(String newSessionId) {
    sessionId = newSessionId;
}

protected void logOut() {
    new RequestLogOutTask().execute(SignedActivity.LOG_OUT_PATH);
    setSessionId("");
    Intent intent = new Intent(this, SignInActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra("logOut", true);
    startActivity(intent);
}

public void onClickLogOut(View v) {
    logOut();
}

@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putString("sessionId", sessionId);
    super.onSaveInstanceState(savedInstanceState);
}

class RequestLogOutTask extends AsyncTask<String, Void, Void> {
    protected Void doInBackground(String... params) {
        (new RESTClient(Constants.REST_URL)).post(params[0], "");
        return null;
    }
}

}

我的问题是,我有时需要FragmentActivity而不是活动登录后。所以,我要创建从FragmentActivity扩展SignedFragmentActivity,这是基本相同SignedActivity(仅限父母是不同的)。所以,我有重复的code - 如何避免它

My problem is that I sometimes need FragmentActivity instead of Activity after login. So I have to create SignedFragmentActivity which extends from FragmentActivity and it is basically the same as SignedActivity (only parent is different). So I have duplicate code - how to avoid it ?

推荐答案

您可以将您的 SignedActivity code到 ActivityHelper 类:

You can move your SignedActivity code to ActivityHelper class:

class ActivityHelper {
    public static String initializeSessionId(Bundle savedInstanceState, Activity activity) {
        if (savedInstanceState != null) {
            String newSessionId = savedInstanceState.getString("sessionId");
            if (newSessionId instanceof String) {
                return newSessionId;
            }
        }
        Bundle extras = activity.getIntent().getExtras();
        if (extras != null) {
            String newSessionId = extras.getString("sessionId");
            if (newSessionId instanceof String) {
                return newSessionId;
            }
        }
        return null;
    }

    ...
}

现在,你可以在每个活动使用它实例(即 FragmentActivity

Now you can use it in every Activity instance (i.e. FragmentActivity):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    sessionId = ActivityHelper.initializeSessionId(savedInstanceState, this);

    finish();
}
...

这篇关于抽象类扩展了多种类型的活动的Java和Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 01:18