1

我有两个要在两者之间切换但还要保留数据的活动。我目前能够从主活动导航到结果活动并绘制一些数据,然后我可以按后退按钮返回到主活动但如果我想再次前进到结果活动(图表)所有数据消失了,图表是空白的。我应该如何切换活动?

主活动中的转发按钮方法:

public void NextBtn(View view)  {
  System.out.println("Next Button Pressed in Main Activity");
  Intent next = new Intent(this, Results.class);
  //this extra is used to know what button called Results activity, if the call if from the NextBtn
  //part of the Results activity onCreate method should not be run since it will crash.
  next.putExtra("what", 1); 
  startActivity(next);
}

结果活动中的后退按钮方法:

public void BackBtn (View view) {
  onBackPressed();  
}

完整的结果活动代码:

package com.example.test;
import java.text.DecimalFormat;
import java.util.Arrays;
import com.androidplot.series.XYSeries;
import com.androidplot.xy.LineAndPointFormatter;
import com.androidplot.xy.SimpleXYSeries;
import com.androidplot.xy.XYPlot;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;

public class Results extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        System.out.println("Results Activity Started");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_results);

        Intent WhatBtn = getIntent();
        int what = WhatBtn.getIntExtra("what", 0);
        //if next button was pressed, break out of onCreate.
        if (what == 1) {
            System.out.println("In If Statement");
            return;
        }

        Intent intent = getIntent();
        double[] gain = intent.getDoubleArrayExtra("gainData");
        double[] phase = intent.getDoubleArrayExtra("phaseData");
        double[] X_axis = intent.getDoubleArrayExtra("Xaxis");

        XYPlot PhasePlot = (XYPlot) findViewById(R.id.PhasePlot);
        XYPlot GainPlot = (XYPlot) findViewById(R.id.GainPlot);

        // Create a couple arrays of y-values to plot:
        //loop is needed to get int[] to number[] and to interleave X & Y values
        Number[] gainNumbers = new Number[2*gain.length];
        Number[] phaseNumbers = new Number[2*phase.length];

        for (int i=1; i<=gain.length; i++) {
            gainNumbers[2*(i-1)] = X_axis[i-1];
            gainNumbers[(2*i)-1] = gain[i-1];
            phaseNumbers[2*(i-1)] = X_axis[i-1];
            phaseNumbers[(2*i)-1] = phase[i-1];
        }

        // Turn the above arrays into XYSeries':
        XYSeries series1 = new SimpleXYSeries(
                Arrays.asList(gainNumbers),          // SimpleXYSeries takes a List so turn our array into a List
                SimpleXYSeries.ArrayFormat.XY_VALS_INTERLEAVED, // Y_VALS_ONLY means use the element index as the x value
                "Series1");                             // Set the display title of the series

        // same as above
        XYSeries series2 = new SimpleXYSeries(Arrays.asList(phaseNumbers), SimpleXYSeries.ArrayFormat.XY_VALS_INTERLEAVED, "Series2");

        // Create a formatter to use for drawing a series using LineAndPointRenderer:
        LineAndPointFormatter series1Format = new LineAndPointFormatter(
                Color.rgb(0, 200, 0),                   // line color
                Color.rgb(0, 100, 0),                   // point color
                null);                                  // fill color (none)

        // add a new series' to the xyplot:
        GainPlot.addSeries(series1, series1Format);

        // same as above:
        PhasePlot.addSeries(series2,
                new LineAndPointFormatter(Color.rgb(0, 0, 200), Color.rgb(0, 0, 100), null));

        // reduce the number of range labels
        GainPlot.setTicksPerRangeLabel(3);

        // by default, AndroidPlot displays developer guides to aid in laying out your plot.
        // To get rid of them call disableAllMarkup():
        GainPlot.disableAllMarkup();
        //Lable plots
        GainPlot.setRangeLabel("Reflection Coefficient");
        GainPlot.setDomainLabel("Frequency (MHz)");
        PhasePlot.setRangeLabel("Phase (Degrees)");
        PhasePlot.setDomainLabel("Frequency (MHz)");
        PhasePlot.setTitle("Phase");
        GainPlot.setTitle("Gain");
        //Remove Legend
        GainPlot.getLayoutManager().remove(GainPlot.getLegendWidget());
        PhasePlot.getLayoutManager().remove(PhasePlot.getLegendWidget());
        //adust margins to avoid axis being cut off
        GainPlot.getGraphWidget().setGridPaddingRight(10);
        GainPlot.getGraphWidget().setGridPaddingTop(4);
        PhasePlot.getGraphWidget().setGridPaddingRight(10);
        PhasePlot.getGraphWidget().setGridPaddingTop(4);
        // only display whole numbers in domain labels
        GainPlot.getGraphWidget().setDomainValueFormat(new DecimalFormat("0"));
        PhasePlot.getGraphWidget().setDomainValueFormat(new DecimalFormat("0"));
    }

    public void start_sweep(View view) {
        //how to call start_sweep method in main activity from results activity
    }

    public void BackBtn (View view) {
        onBackPressed();    
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.results, menu);
        return true;
    }

}
4

1 回答 1

0

您可以在 Manifest 文件的 Result 活动中设置 android:launchMode="singleTop"。所以现在,如果您已经创建了一个活动(结果),那么向它发送另一个意图不会创建一个新活动,而是将前一个活动放到前台
查看更多信息

于 2013-08-05T17:30:43.957 回答