Question

My Android application has a main Intent where the user logs in. After that, it loads another Intent with arrow buttons, some label, a progress bar and some input components that alternates display while user clicks buttons to go to another questions (a research form). For each question, the labels changes and some components hides while others shows up. But if user rotates the device, the Intent kinds of resets, because it returns to first question.

The code that takes to second Intent is:

    public void onClick(View v) 
    {
        if ((!selectedFormCode.equalsIgnoreCase("")) && (!userCode.getText().toString().equals("")))
        {
            Intent questionScreen = new Intent();
            questionScreen.setClassName("com.android.artemis", "com.android.artemis.QuestionScreen");

            func.ShowMessage("Aguarde,\nCarregando o Formulário...");

            // envia as configurações para a tela de entrevista
            questionScreen.putExtra("formProperties", formProperties.toString()); 
            questionScreen.putExtra("selectedFormCode", selectedFormCode);
            questionScreen.putExtra("selectedRegionCode", selectedRegionCode);
            questionScreen.putExtra("selectedSubRegionCode", selectedSubRegionCode);
            questionScreen.putExtra("selectedRegionLabel", selectedRegionLabel);
            questionScreen.putExtra("selectedSubRegionLabel", selectedSubRegionLabel);
            questionScreen.putExtra("userRegistrationCode", userCode.getText().toString());

            startForm.setEnabled(false); // evita que o usuário clique mais de uma vez

            startActivity(questionScreen);

            startForm.setEnabled(true);
        }
        else
        {
            func.ShowMessage("Complete todas as Informações antes de Continuar!");
        }
    }

The code that triggers when second Intent loads is:

        super.onAttachedToWindow();
        try {
            // recebe informações vindas da tela inicial
            selectedFormCode = getIntent().getStringExtra("selectedFormCode");
            selectedRegionCode = getIntent().getStringExtra("selectedRegionCode");
            selectedSubRegionCode = getIntent().getStringExtra("selectedSubRegionCode");
            selectedRegionLabel = getIntent().getStringExtra("selectedRegionLabel");
            selectedSubRegionLabel = getIntent().getStringExtra("selectedSubRegionLabel");
            userRegistrationCode =  getIntent().getStringExtra("userRegistrationCode");
            formProperties = new JSONObject(getIntent().getStringExtra("formProperties"));
            //ArrayList<JSONObject> formList;
            //JSONObject mainData = new JSONObject(func.getTextAssetFile("forms.dat"));
            //formList = func.getJSONArrayList(mainData.get("forms").toString()); 
            //formProperties = formList.get(func.getIndexFromObject(formList, selectedFormCode));

            // inicializa a tela
            formLabel.setText(formProperties.get("l").toString()); // label         
            customEdit.clearFocus();
            // inicializa variáveis a serem usadas na entrevista
            totalQuestion = (Integer) formProperties.get("total");
            questionAnswer = new String[totalQuestion];
            subQuestionAnswer = new String[totalQuestion];
            questionsData = formProperties.getJSONArray("questions");
            // recupera a informação de quantidade de entrevistas feitas deste formulário, por este entrevistador
            SharedPreferences settings = getSharedPreferences(selectedFormCode, 0);
            totalInterview = settings.getInt(userRegistrationCode, 0);
            // inicia uma nova entrevista depois que configura a tela
            startNewInterview();
/*          
            // Inicialização de uma sessão do dropbox para sincronização de arquivos de entrevistas
            AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
            AndroidAuthSession session = new AndroidAuthSession(appKeys, ACCESS_TYPE);
            mDBApi = new DropboxAPI<AndroidAuthSession>(session);
            //
            SharedPreferences dbas = getSharedPreferences("SPM", 0);
            AccessTokenPair access = new AccessTokenPair(dbas.getString("dropboxKey", ""),dbas.getString("dropboxSecret", "")); 
            mDBApi.getSession().setAccessTokenPair(access);
            mDBApi.getSession().startAuthentication(this);
*/          
        } 
        catch (JSONException e) 
        {
            e.printStackTrace();
            func.ShowMessage("Não foi possível decodificar as propriedades do formulário de pesquisa. \nErro Fatal! \nLibere mais Memória!!");
        }

    }

and

public void startNewInterview() // inicializa uma nova entrevista
{
    // reinicia a barra de progresso
    currentQuestion = 1;
    progressBar.setMax(totalQuestion);
    progressBar.setProgress(currentQuestion);
    // limpar as variáveis de respostas
    for (int i = 0; i < totalQuestion; i++)
    {
        questionAnswer[i] = "";
        subQuestionAnswer[i] = "";
    }
    // inicializar o relógio
    func.startChronometer();
    startTime = func.getChronometerTime(); 
    // determinat o momento do início da entrevista
    Date now = new Date();;  
    SimpleDateFormat formatTime = new SimpleDateFormat(dateMask);  
    interviewDate = formatTime.format(now);  
    formatTime = new SimpleDateFormat(hourMask);  
    interviewHour = formatTime.format(now);  
    // ir para a primeira questão
    goQuestion(currentQuestion);
}
Was it helpful?

Solution

Add this in your Manifest

<activity
        android:name="MyActivity"
        android:configChanges="orientation|keyboard|keyboardHidden"
        android:screenOrientation="sensor" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top