我们可以在片段中的onResume方法中调用onCreateView方法吗?

时间:2013-08-09 05:50:13

标签: android android-fragments onresume

我正在开发一个我正在使用片段的android应用程序。并且在点击列表项并且其工作正常后,我将语言从英语更改为阿拉伯语。

但是我想要的是所选择的语言即使在我们强制关闭设备应用之后仍然保留在整个应用程序中....但它并没有发生在我身上....

如果我正在使用Activity,那么我打电话

setContentView(R.layout.main); 

在onResume()方法中,它的工作正常。

但我的问题是我们如何在onResume()方法上设置一个带有片段或调用onCreateView方法的视图,因为onCreateView()方法返回自己的视图..所以我已经尝试了很多来解决它,但我无法做到..那么请问有什么方法可以解决它吗?

先谢谢。

这是我的代码:

package com.example.okazz.Settings;

import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.okazz.R;

public class Languages extends Fragment {
// private int current = -1;
View view;
Context ct;
SharedPreferences sp1, sp;
Boolean Ischeck = false;
String User_id, countryid;
Button settings;
String[] arrLanguages = new String[] { "English", "Arabic" };
String req;
ListView lv;
private Locale myLocale;

public Languages(Context c) {

    this.ct = c;
}

/** Called when the activity is first created. */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    view = inflater.inflate(R.layout.languagechange, container, false);

    SharedPreferences sp = getActivity().getSharedPreferences("key", 0);
    User_id = sp.getString("userid=", User_id);
    countryid = sp.getString("countryid", countryid);
    settings = (Button) view.findViewById(R.id.btnsett);
    lv = (ListView) view.findViewById(android.R.id.list);
    lv.setAdapter(new MobileArrayAdapter(ct, arrLanguages));
    settings.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            FragmentManager manager = getFragmentManager();
            manager.popBackStack();
            Log.d("Back to Home", "back button pressed");
        }
    });

    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            // get selected items
            String selectedValue = (String) lv.getAdapter().getItem(
                    position);
            Toast.makeText(ct, selectedValue, Toast.LENGTH_SHORT).show();
            // setResult(RESULT_OK, new Intent().putExtra("language",
            // arrLanguages[position]));
            // Configuration config = new Configuration();
            String lang = null;
            switch (position) {
            case 0:
                lang = "en";
                break;
            case 1:
                lang = "ar";
                break;

            default:

                break;
            }

            changeLang(lang);

            // SharedPreferences sp = getActivity().getSharedPreferences(
            // "check", 0);
            //
            // boolean checked = sp.getBoolean("Ischeck", false);
            //
            // if (checked == true) {
            lv.setItemChecked(position, true);
            // }

        }

    });
    return view;
}

public void loadLocale() {
    String langPref = "Language";
    SharedPreferences prefs = ct.getSharedPreferences("CommonPrefs",
            Activity.MODE_PRIVATE);
    String language = prefs.getString(langPref, "");
    changeLang(language);

}

public void saveLocale(String lang) {
    String langPref = "Language";
    SharedPreferences prefs = ct.getSharedPreferences("CommonPrefs",
            Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(langPref, lang);
    editor.commit();
}

public void changeLang(String lang) {
    if (lang.equalsIgnoreCase(""))
        return;
    myLocale = new Locale(lang);
    Log.v("My Language", myLocale + "");
    saveLocale(lang);
    Locale.setDefault(myLocale);
    android.content.res.Configuration config = new android.content.res.Configuration();
    config.locale = myLocale;
    ct.getResources().updateConfiguration(config,
            ct.getResources().getDisplayMetrics());

}

class MobileArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final String[] values;

    public MobileArrayAdapter(Context context, String[] values) {
        super(context, R.layout.list_changelanguage, values);
        this.context = context;
        this.values = values;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.list_changelanguage,
                parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.textView1);
        ImageView imageView = (ImageView) rowView
                .findViewById(R.id.imageView1);
        textView.setText(values[position]);

        String s = values[position];

        System.out.println("**** Position ****" + s);
        if (lv.isItemChecked(position)) {
            imageView.setVisibility(View.VISIBLE);
            Ischeck = true;
            SharedPreferences sp = getActivity().getSharedPreferences(
                    "check", 0);
            SharedPreferences.Editor sedt = sp.edit();
            sedt.putBoolean("Ischeck", Ischeck);
            sedt.clear();
            sedt.commit();
        } else {
            imageView.setVisibility(View.GONE);

        }

        return rowView;
    }
}

@Override
public void onConfigurationChanged(
        android.content.res.Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (myLocale != null) {
        newConfig.locale = myLocale;
        Locale.setDefault(myLocale);
        getActivity().getResources().updateConfiguration(newConfig,
                getActivity().getResources().getDisplayMetrics());
    }
}

@Override
public void onResume() {
    // TODO Auto-generated method stub
    loadLocale();
    super.onResume();
}}

2 个答案:

答案 0 :(得分:0)

您的问题有点难以阅读,但我认为我明白了:您希望在语言发生变化时重新布局片段。

我认为对于fragements最好的方法是删除片段,然后重新添加一个新实例到您的活动中。

另一个选择是使用活动及其中的片段的onConfigurationChange方法:Changing locale: Force activity to reload resources?

您可以通过以下方式自行解决此事件:

int lang = //the result of your click inside the list item
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();

switch(lang){
    case ARABIC:
        conf.locale = Locale.WHATEVER;
        break;

    case ENGLISH:
        conf.locale = Locale.ENGLISH;
        break;
}
res.updateConfiguration(conf, dm);

答案 1 :(得分:0)

您可以使用 SharedPreferences 保存您的语言偏好,并使用onCreate中的首选语言重新创建视图