Slava
Slava

Reputation: 11

Android. TabActivity, switching between tabs without destroying activity from tabHost

I created an application that is use a TabActivity. Everything is ok till one moment...

Every time the tab are switched the activityes from tabHost are destroyed and recreated again. I loged every state of Activity and found out that when tab is focused are called methosd: onCreatr(), onStart(), onResume(), and whent tab is out of focus are called onStop(), onDestroy()

How can I switch the tabs without destroying and recreating the activity. ???

Sorry for my English :)

Main.Class

package my.pack;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;

public class Main extends TabActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TabHost tabHost = getTabHost();
        TabHost.TabSpec spect;
        Intent intent;

        intent = new Intent().setClass(this, ListDevice.class);
        spect = tabHost.newTabSpec("test").setIndicator("Device").setContent(intent);
        tabHost.addTab(spect);

        intent = new Intent().setClass(this, ListServer.class);
        spect = tabHost.newTabSpec("test").setIndicator("Server").setContent(intent);
        tabHost.addTab(spect);

        tabHost.setCurrentTab(0);
    }
}

ListServer.class (used in tabHost)

package my.pack;

import java.io.File;
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

public class ListServer extends ListActivity {

    private File file = new File(".");
    private ArrayList<File> values = new ArrayList<File>();
    private CopyOfMyPerformanceArrayAdapter adapter;

    public void onCreate(Bundle icicle) {
        Log.d("test","server");
        super.onCreate(icicle);
        for (File f: file.listFiles()) {
            if (f.isDirectory()) values.add(f);
        }
        for (File f: file.listFiles()) {
            if (f.isFile()) values.add(f);
        }

        View footer = getLayoutInflater().inflate(R.layout.footer, null);
        getListView().addFooterView(footer);

        adapter = new CopyOfMyPerformanceArrayAdapter(this,values);
        setListAdapter(adapter);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        try{
        File file = (File) getListAdapter().getItem(position);

        if (file.isDirectory()) {
            adapter.clear();
            for (File f: file.listFiles()) {
                values.add(f);
            }
        adapter.notifyDataSetChanged();
        }} catch(Exception e) { Log.d("opa", "server");}

    }


    public void onRestart(){
        Log.d("ftp","onRestart");
        super.onRestart();
    }
    public void onStart(){
        Log.d("ftp","onStart");
        super.onStart();
    }
    public void onResume(){
        Log.d("ftp","onResume");
        super.onResume();
    }
    public void onPouse(){
        Log.d("ftp","onpouse");
        super.onPause();
    }
    public void onStop(){
        Log.d("ftp","onStop");
        super.onStop();
    }
    public void onDestroy(){
        Log.d("ftp","onDestroy");
        super.onDestroy();
    }
}

Upvotes: 1

Views: 3529

Answers (4)

yasir
yasir

Reputation: 21

use differenct key fore each tab spec,

replace... this

spect = tabHost.newTabSpec("test")
spect = tabHost.newTabSpec("test")

with

spect = tabHost.newTabSpec("test1")
spect = tabHost.newTabSpec("test2")

this work for me..

Upvotes: 1

Magakahn
Magakahn

Reputation: 508

I think moveTaskToBack on onDestroy should work.

Upvotes: 0

Eugene Chumak
Eugene Chumak

Reputation: 3192

Your tabs have the same tag - "test". Just specify different tags and everything should be ok.

Upvotes: 0

D-Dᴙum
D-Dᴙum

Reputation: 7890

You might want to consider using Fragments instead to create your Tabs as described here However the example shown there also destroys the tab content (i.e. the Fragment) when it is swapped (in the onTabChanged()). Luckily you can alter this behaviour. If you notice the example uses FragmentTransaction.add() and FragmentTransaction.remove() but you can change these to FragmentTransaction.show() and FragmentTransaction.hide(). This way your Fragment activity is not destroyed when switched out-of-view

Upvotes: 1

Related Questions