如何在单独的类中为我的所有活动创建显式意图?

时间:2015-08-07 16:28:22

标签: android android-intent android-context

我有一个按钮,它会打开一个填充了其他活动页面的布局,当我触摸其中任何一个时,它会通过明确的意图跳转到它:

Martin R

我可以在每个页面上创建多个onClick方法,如下所示:

public class Tab_Modes extends ListFragment {
private ArrayList<NameImg> items = new ArrayList<NameImg>();
private int crtMode;
private String str;
public Tab_Modes() {
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    //  Activity mActivity = activity;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle arguments = getArguments();
    crtMode = arguments.getInt("position");
    str  = arguments.getString("frag");
    items.clear();
    switch (crtMode) {
        case 0:
            items.add(new NameImg("Manual", "Simple camera Control", R.drawable.hand));
            items.add(new NameImg("Bulb", "Long Exposure", R.drawable.bulb));
            items.add(new NameImg("Time Lapse", "Frame by Frame Movie", R.drawable.timelapse));
            items.add(new NameImg("HDR", "High Dinamic Range", R.drawable.hdr));
            items.add(new NameImg("IR", "Infra RED Control", R.drawable.ir));
            break;
        case 1:
            items.add(new NameImg("Triggered", "Trigger camera ", R.drawable.triggrered));
            items.add(new NameImg("Dark Room", "Long Exposure", R.drawable.darkroom));
            items.add(new NameImg("Lightning", "Frame by Frame Movie", R.drawable.lightning));
            break;
        case 2:
            items.add(new NameImg("Bullet", "Simple camera Control", R.drawable.bullet));
            items.add(new NameImg("Water drops", "Long Exposure", R.drawable.waterdrops));
            break;
        case 3:
            items.add(new NameImg("Manual", "Simple camera Control", R.drawable.pan_manual));
            items.add(new NameImg("Programmed", "Long Exposure", R.drawable.pan_auto));
            items.add(new NameImg("Star Tracking", "Long Exposure", R.drawable.starstracker));
            break;
        case 4:
            items.add(new NameImg("1", "USB1", R.drawable.usb));
            items.add(new NameImg("2", "USB2", R.drawable.waterdrops));
            break;
    }
    setListAdapter(new ModesItemAdapter(getActivity(), R.layout.my_list_item, items));
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = super.onCreateView(inflater, container, savedInstanceState);
    //ListView myLV = null;
    //myLV = (ListView) myLV.findViewById(R.id.ItemView);
    //myLV.setItemChecked(0, true);
    return v;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

}

public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Toast.makeText(getActivity(), "selected Frag :" + str, Toast.LENGTH_LONG).show();
    FragmentManager fm = getFragmentManager();
    ModeControls myControls;
    myControls = (ModeControls) fm.findFragmentById(R.id.fragment_manual);
    if (myControls == null || !myControls.isInLayout()) {//if the fragment dosen't exists we create it
        Bundle arguments = new Bundle();
        arguments.putString("myTxt", "selected item :" + position + " ID :" + id);
        FragmentTransaction ft = fm.beginTransaction();
        myControls = new ModeControls();
        myControls.setArguments(arguments);
        ft.add(R.id.pager, myControls).commit();
    }
}

但是,编写代码需要花费很多时间,而我想创建单独的类,根据需要从每个活动中创建意图。

package com.grozeaion.www.gvicameraremotecontrol;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;


public class ModeControls extends Fragment {
    TextView textDetail;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
        Bundle arguments = getArguments();
        View view = inflater.inflate(R.layout.fragment_manual_mode, viewGroup, false);
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        textDetail = (TextView) view.findViewById(R.id.text_detail);
        textDetail.setText(arguments.getString("myTxt"));
        Toast.makeText(getActivity(), "ModeControls " + arguments.getString("myTxt"), Toast.LENGTH_SHORT).show();
        //Toast.makeText(getActivity(), "ModeControls " + getResources().getConfiguration().orientation , Toast.LENGTH_SHORT).show();
        return view;
    }
}

} 问题是我无法弄清楚如何知道&#34;从

中调用的当前上下文

1 个答案:

答案 0 :(得分:1)

你可以简单地称之为:

new MenuDetails().createIntent(this, SecondActivity.class);

其中'this'是您调用此函数的活动的上下文。