2013-08-05 24 views
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; 
    } 

} 

回答

0

您可以设置机器人:launchMode清单文件中的Result活动中显示=“singleTop”。 所以,现在,如果你已经创建了一个活动的(结果)一次,然后发送另一个意图,它不会创建一个新的活动,但取得前一个前台
this更多信息

+0

谢谢您的回复,这似乎没有帮助,但我确实意识到我过于复杂了。我只是简单地让NextBtn发送与第一次被称为图形数据完全相同的Intent。只需要使一些数据全球化。 – user2644213

+0

但是,再次发送相同的数据会导致不必要的数据流,而不仅仅是将带有数据的旧活动带到前面并且只需要刷新(带到前面)。但是,如果你的解决方案足够好的话:) – Kostek

相关问题