刷新标签(选项卡式活动)

时间:2018-03-03 13:54:01

标签: android android-intent android-activity tabs refresh

当我从"搜索活动"回来时,我试图刷新我的三个标签页。我正在寻找破坏它们并在onPause中重新创建它们但是不起作用。 "搜索活动"是我的主要活动上的操作栏中的活动。当我在我的"搜索活动"中调用此方法时我想刷新所有标签

    public void closeSearchActivity() {

    SharedPreferences.Editor editor = preferencias.edit();
    editor.putString("actual", xmlActual.toString());
    editor.putString("forecast", xmlForecast.toString());
    editor.commit();
    finish();
}

我的主要活动

package com.example.javier.meteojavier;

import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import java.io.File;


public class MT_Main extends AppCompatActivity {

private static final int COD_BUSCAR = 100;
private static final int COD_DEFECTO = 200;
private static final int COD_HOME = 300;
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
File rutaCurrent;
File rutaForecast;
Fragment mt1;
Fragment mt2;
Fragment mt3;
Bundle argsInicial;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mt__main);

    // Comprobar que archivo abrir la primera vez
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

    mt1 = new MT_Tab1Actual();
    mt2 = new MT_Tab2Prediccion();
    mt3 = new MT_Tab3Historico();

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == COD_BUSCAR) {
        if (resultCode == RESULT_OK) {

            if (data.hasExtra("ruta")) {
                //ruta = new File(data.getExtras().getString("ruta"));
                //Log.e("ruta", ruta.toString());
            }
        }
    }
    if (requestCode == COD_HOME) {
        if (resultCode == RESULT_OK) {
            if (data.hasExtra("forecast") && data.hasExtra("actual")) {
                argsInicial = new Bundle();
                argsInicial.putString("actual", data.getExtras().getString("actual"));
                argsInicial.putString("forecast", data.getExtras().getString("forecast"));
                mt1.setArguments(argsInicial);
                mt2.setArguments(argsInicial);
            }
        }
    }
}

// =========================== MENU THINGS // ===========================
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_mt__main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

        case R.id.action_settings:
            Intent intentPreferencias = new Intent(getApplicationContext(), MT_Preferencias.class);
            startActivity(intentPreferencias);
            break;
        case R.id.action_buscar:
            Intent intentBuscar = new Intent(getApplicationContext(), MT_Search.class);
            startActivityForResult(intentBuscar, COD_BUSCAR);
            break;
    }
    return super.onOptionsItemSelected(item);
}


public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return mt1;
            case 1:
                return mt2;
            case 2:
                return mt3;
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {

        switch (position) {

            case 0:
                return getString(R.string.tittle_Actual);
            case 1:
                return getString(R.string.tittle_Tiem);
            case 2:
                return getString(R.string.tittle_Hist);
        }
        return null;
    }
}

public void onResume() {

    super.onResume();
}

}

谢谢大家!抱歉我的英文!

1 个答案:

答案 0 :(得分:0)

从搜索活动

返回时,您应该刷新适配器
viewPage.setAdapter( adapter );