为什么我的某项活动的转换有所不同?

时间:2016-11-03 03:38:00

标签: android android-transitions

我正在研究android,它出现在我测试的每个版本中的所有设备,模拟器和物理上。除了从任何地方进入主屏幕之外,从屏幕到屏幕的转换都是相同的。这是片段代码。

public class NavBarFragment extends Fragment implements View.OnClickListener{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.nav_bar_frag, container, false);

        Button homeButton = (Button) v.findViewById(R.id.homeButton);
        Button vaultButton = (Button) v.findViewById(R.id.vaultButton);
        Button shopButton = (Button) v.findViewById(R.id.shopButton);
        Button accInfoButton = (Button) v.findViewById(R.id.accInfoButton);

        homeButton.setOnClickListener(this);
        vaultButton.setOnClickListener(this);
        shopButton.setOnClickListener(this);
        accInfoButton.setOnClickListener(this);

        return v;
    }


    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {

            case R.id.homeButton:
                intent = new Intent(getActivity(), TasksScreenActivity.class);
                break;

            case R.id.vaultButton:
                intent = new Intent(getActivity(), ChestActivity.class);
                break;

            case R.id.shopButton:
                intent = new Intent(getActivity(), ShopActivity.class);
                break;

            case R.id.accInfoButton:
                intent = new Intent(getActivity(), AccountActivity.class);
                break;

            default:
                System.err.println("Error onClick in NavBarFragment");
                break;
        }
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        startActivity(intent);
    }
}

这是清单的相关部分

<activity android:name=".TasksScreenActivity"
    android:launchMode="singleTask">
</activity>

<activity android:name=".ChestActivity"
    android:launchMode="singleTask">
</activity>

<activity android:name=".ShopActivity"
    android:launchMode="singleTask">
</activity>

<activity android:name=".AccountActivity"
    android:launchMode="singleTask">
</activity>

据我所知,我对所有这些都做了同样的事情,它应该是一致的。对于所有其他屏幕,动画显示淡入,就像它弹出一样,对于主页,它显示淡出,就像它缩小到中间然后消失。

我的问题是,它们不一致,我不关心结束过渡是什么,只要它们都是一样的。

3 个答案:

答案 0 :(得分:4)

Intent intent = new Intent(activity, clazz);
activity.startActivity(intent);
activity.overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);

使用overridePendingTransition为所有新观看活动设置转场

答案 1 :(得分:4)

提供的答案是正确的,但如果您在活动过渡期间不想要任何动画,也可以这样做。

Intent intent = new Intent(activity, clazz);
activity.startActivity(intent);
activity.overridePendingTransition(0, 0);

无需动画时可能会有所帮助。

答案 2 :(得分:4)

您可以使用默认动画

function MessageHandler(context, event) {
    // var nlpToken = "xxxxxxxxxxxxxxxxxxxxxxx";//Your API.ai token
    // context.sendResponse(JSON.stringify(event));
    sendMessageToApiAi({
        message : event.message,
        sessionId : new Date().getTime() +'api',
        nlpToken : "3626fe2d46b64cf8a9c0d3bee99a9sb3",
        callback : function(res){
            //Sample response from apiai here.
            context.sendResponse(JSON.parse(res).result.fulfillment.speech);
        }
    },context)
}

function sendMessageToApiAi(options,botcontext) {
    var message = options.message; // Mandatory
    var sessionId = options.sessionId || ""; // optinal
    var callback = options.callback;
    if (!(callback && typeof callback == 'function')) {
       return botcontext.sendResponse("ERROR : type of options.callback should be function and its Mandatory");
    }
    var nlpToken = options.nlpToken;

    if (!nlpToken) {
       if (!botcontext.simpledb.botleveldata.config || !botcontext.simpledb.botleveldata.config.nlpToken) {
           return botcontext.sendResponse("ERROR : token not set. Please set Api.ai Token to options.nlpToken or context.simpledb.botleveldata.config.nlpToken");
       } else {
           nlpToken = botcontext.simpledb.botleveldata.config.nlpToken;
       }
    }
    var query = '?v=20150910&query='+ encodeURIComponent(message) +'&sessionId='+sessionId+'&timezone=Asia/Calcutta&lang=en    '
    var apiurl = "https://api.api.ai/api/query"+query;
    var headers = { "Authorization": "Bearer " + nlpToken};
    botcontext.simplehttp.makeGet(apiurl, headers, function(context, event) {
       if (event.getresp) {
           callback(event.getresp);
       } else {
           callback({})
       }
    });
}

/** Functions declared below are required **/
function EventHandler(context, event) {
    if (!context.simpledb.botleveldata.numinstance)
        context.simpledb.botleveldata.numinstance = 0;
    numinstances = parseInt(context.simpledb.botleveldata.numinstance) + 1;
    context.simpledb.botleveldata.numinstance = numinstances;
    context.sendResponse("Thanks for adding me. You are:" + numinstances);
}

function HttpResponseHandler(context, event) {
    // if(event.geturl === "http://ip-api.com/json")
    context.sendResponse(event.getresp);
}

function DbGetHandler(context, event) {
    context.sendResponse("testdbput keyword was last get by:" + event.dbval);
}

function DbPutHandler(context, event) {
    context.sendResponse("testdbput keyword was last put by:" + event.dbval);
}